這兩個類聲明有什麼區別? 「對象」是做什麼的?Python新手:頭疼面向對象編程
class className(object): pass class className: pass
當我運行下面的代碼爲什麼我得到這個錯誤: 「時不採取任何(給出1)參數」
class Hobbs(): def represent(): print "Hobbs represent!" represent = classmethod(represent) Hobbs.represent()
爲什麼 「Foo.class_foo()」 給沒有錯誤即使我沒有傳遞函數的參數。
class Foo(object): @staticmethod def static_foo(): print "static method" @classmethod def class_foo(cls): print "Class method. Automatically passed the class: %s" % cls Foo.static_foo() Foo.class_foo()
爲什麼我運行下面的代碼時會出現此錯誤?
class Foo(object): def static_foo(): print "static method" static_foo = staticmethod(static_foo) def class_foo(cls): print "Class method. Automatically passed the class: %s" % cls class_foo = classmethod(class_foo) Foo.static_foo() Foo.class_foo()
"TypeError: unbound method static_foo() must be called with Foo instance as first argument (got nothing instead)"
您應該閱讀[Python教程](http://docs.python.org/tutorial/)以熟悉Python中的這些基類。 – BrenBarn