来自课外的python调用方法

时间:2016-07-09 14:40:16

标签: python class

我怎样才能从外面的python调用方法中找到类?

class C():    
    def write():
        print 'Hello worl'

我认为>>> x = C>>> x.write()必须有效,但事实并非如此。

2 个答案:

答案 0 :(得分:3)

你的定义中不需要自我吗?

class C(object):    
    def write(self):
        print 'Hello world'

现在应该没问题,即

x = C()
x.write()

答案 1 :(得分:0)

当你在定义x的地方时,你忘了在课后放置parantheses,这样做x字面上等于C类而不是对象C.

class C:
    def write():
        print "Hello worl"

x = C() # Notice the parantheses after the class name.
x.write() # This will output Hello worl.
相关问题