你是否認為 Python 類中的宣告等同於 __init__ 方法中的宣告?
類中任何位置(除 __init__ 外)的宣告和 __init__ 方法中的宣告是不同的。以下程式碼對此進行了驗證。
示例
import sys class foo(): print 'within class' def __init__(self): print 'within init' def do_smthng(self): print 'do something' def main(): f=foo() f.do_smthng() return 0 if __name__ == '__main__': sys.exit( main() )
輸出
within class within init do something
廣告