0

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.

2 Answers2

4

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()
pathoren
  • 1,634
  • 2
  • 14
  • 22
0

When you where defining x, you forgot to put the parantheses after the class, this made so x literally was equals to the class C and not the object C.

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

x = C() # Notice the parantheses after the class name.
x.write() # This will output Hello worl.
O. Edholm
  • 2,044
  • 2
  • 12
  • 13