Tomáš Vavřinka
Tomáš Vavřinka

Reputation: 632

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.

Upvotes: 0

Views: 2645

Answers (2)

O. Edholm
O. Edholm

Reputation: 2412

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.

Upvotes: 0

pathoren
pathoren

Reputation: 1666

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()

Upvotes: 4

Related Questions