Reputation: 9492
This is a sample code that i found from one of the python class tutorial.
class MyClass:
i = 12345
def f(self):
return 'hello world'
print MyClass.f
print MyClass.i
Once i run this, i am expecting the output result of "hello world" and "12345". But instead i am getting this
>>>
<unbound method MyClass.f>
12345
>>>
why is it not giving me 'hello world'? How do i change my code so that it will print out "hello world"? P.S i have no clue about python classes and methods and just started learning.
Upvotes: 2
Views: 247
Reputation: 2679
Create an instance of MyClass first.
test = MyClass()
print test.f()
print MyClass.i
You don't need to create an instance of MyClass for i, because it is a class member, not an instance member.
Upvotes: 7
Reputation: 11070
Always a function is called by its name, which is represented by ()
. So use
MyClass.f()
Upvotes: 1