Tomáš Vavřinka Tomáš Vavřinka - 1 year ago 81
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
>>> x.write()
must work but it doesn't.

Answer Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download