python - python类中的声明等同于_init_?

标签 python

我想知道放在 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/

相关文章:

python - 是否可以使用诱变剂将 ID3 标签添加到 m4a 文件?

python - 使用 Pandas 合并数据框

python - 为什么web.py中打印结果是 "favicon.ico"

Python 3 struct.pack() 打印奇怪的字符

python - 使用 lambda 的 Pandas 聚合警告(FutureWarning : using a dict with renaming is deprecated)

python - 从 python 或 bash 批量填写 PDF 表单

python - 是否有相当于 python 的 virtualenv 的 GO?

python - 在 python 脚本中运行 sqoop

python - 如何使用 qmake 在 OSX 10.6 上链接为 .so 而不是 .dylib

python - 匹配数字或 100% 但不是 99% 的正则表达式