Skip to content Skip to sidebar Skip to footer

Python Class Attribute Referencing

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.

Solution 1:

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.

Solution 2:

Post a Comment for "Python Class Attribute Referencing"