我想知道Python是否有与C相似的问题,关于某些代码元素的执行顺序.
例如,我知道在C中有时会说不保证某个变量在另一个变量之前被初始化.或者仅仅因为一行代码高于另一行代码,不能保证它在它下面的所有代码之前实现.
对Python来说是一样的吗?就像我打开一个数据文件,读入数据,关闭文件,然后做其他的事情,我知道文件是在关闭文件执行后的行之前关闭的吗?
我问的原因是因为我试图读取一个大的数据文件(1.6GB)并使用这个python模块特定于我对数据所做的工作.当我运行此模块时,我收到此错误消息:
File "/glast01/software/ScienceTools/ScienceTools-v9r15p2-SL4/sane/v3r18p1/python/GtApp.py", line 57, in run
input, output = self.runWithOutput(print_command)
File "/glast01/software/ScienceTools/ScienceTools-v9r15p2-SL4/sane/v3r18p1/python/GtApp.py", line 77, in runWithOutput
return os.popen4(self.command(print_command))
File "/Home/eud/jmcohen/.local/lib/python2.5/os.py", line 690, in popen4
stdout, stdin = popen2.popen4(cmd, bufsize)
File "/Home/eud/jmcohen/.local/lib/python2.5/popen2.py", line 199, in popen4
inst = Popen4(cmd, bufsize)
File "/Home/eud/jmcohen/.local/lib/python2.5/popen2.py", line 125, in __init__
self.pid = os.fork()
OSError: [Errno 12] Cannot allocate memory
>>>
Exception exceptions.AttributeError: AttributeError("Popen4 instance has no attribute 'pid'",) in <bound method Popen4.__del__ of <popen2.Popen4 instance at 0x9ee6fac>> ignored
Run Code Online (Sandbox Code Playgroud)
我假设它与我读入的数据大小有关(它有17608310行和22列).我想也许如果我在读完数据之后关闭了我打开的文件,这会有所帮助,但事实并非如此.这让我想到了执行代码行的顺序,因此我的问题.
谢谢
Tim*_*ker 11
我能想到的唯一可能让一些人感到惊讶的是:
def test():
try:
return True
finally:
return False
print test()
Run Code Online (Sandbox Code Playgroud)
输出:
False
Run Code Online (Sandbox Code Playgroud)
finally
条款实际上是最后执行的,即使return
它们之前有一个语句.但是,这不是Python特有的.