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.

asked Jul 9, 2016 at 14:40
1
  • 1
    You forgot the parentheses after C. Type x = C() instead of x = C. Commented Jul 9, 2016 at 14:42

2 Answers 2

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()
answered Jul 9, 2016 at 14:43
Sign up to request clarification or add additional context in comments.

Comments

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.
answered Jul 9, 2016 at 15:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.