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.
Always a function is called by its name, which is represented by
()
. So useMyClass.f()
Create an instance of MyClass first.
You don't need to create an instance of MyClass for i, because it is a class member, not an instance member.