Tomáš Vavřinka Tomáš Vavřinka - 5 months ago 16
Python Question

python call method from outside the class

how can I in python call method from outside which is situated inside the class?

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


I thought that
>>> x = C
and
>>> x.write()
must work but it doesn't.

Answer

Dont you need to have self in your definition?

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

Now it should be fine, i.e.

x = C()
x.write()