我想知道放在 python 类顶部的声明是否等同于 __init__
中的语句?例如
import sys
class bla():
print 'not init'
def __init__(self):
print 'init'
def whatever(self):
print 'whatever'
def main():
b=bla()
b.whatever()
return 0
if __name__ == '__main__':
sys.exit( main() )
输出是:
not init
init
whatever
作为旁注,现在我还得到:
Fatal Python error: PyImport_GetModuleDict: no module dictionary!
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
关于这是为什么的任何想法?提前致谢!
最佳答案
不,这不等价。声明print 'not init'
在类 bla
时运行正在定义,甚至在您实例化 bla
类型的对象之前.
>>> class bla():
... print 'not init'
... def __init__(self):
... print 'init'
not init
>>> b = bla()
init
关于python - python类中的声明等同于_init_?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10361810/