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.
asked Jul 9, 2016 at 14:40
Tomáš Vavřinka
63311 silver badges30 bronze badges
2 Answers 2
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()
answered Jul 9, 2016 at 14:43
pathoren
1,6662 gold badges14 silver badges23 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Jul 9, 2016 at 15:04
O. Edholm
2,4322 gold badges14 silver badges13 bronze badges
Comments
lang-py
x = C()instead ofx = C.