Understanding Class Variables, Instance Variables, Class Methods, and Static Methods in Python
This article explains Python's class (static) variables versus instance variables, demonstrates how they are accessed and overridden, and details the differences between class methods, instance methods, and static methods, including naming conventions for private and protected attributes, supplemented with clear code examples and expected outputs.
In Python, variables declared directly in a class are class (static) variables, while variables assigned in __init__ and bound to self are instance (member) variables.
Class variables can be accessed via the class name or an instance; instance variables are accessed only through an instance. When both exist with the same name, the instance attribute shadows the class attribute when accessed via the object, but the class attribute remains reachable via the class.
<code>class Person(object):
# class variables
name = "haha"
age = "19"
height = 170
def __init__(self, name, age, weight):
# instance variables
self.name = name
self.age = age
self.weight = weight
def sayHeight(self):
print(self.height)
per = Person("xiaoming", 19, 50)
print(per.name) # xiaoming
print(Person.name) # haha
per.height = 190
print(per.height) # 190
print(Person.height) # 170
print('*********')
Person.height = 200
print(Person.height) # 200
print(per.height) # 190
per.sayHeight()
</code>The output of the above code is:
<code>xiaoming
haha
190
170
*********
200
190
190
</code>Class methods are defined with the @classmethod decorator; they receive the class itself as the first argument ( cls ) and can be called via the class name.
Instance (member) methods receive the instance ( self ) as the first argument and are called on an object.
Static methods are defined with the @staticmethod decorator; they do not receive an implicit first argument and can be called on the class or an instance without needing to instantiate the class.
<code>class A(object):
def foo(self, x):
print("executing foo(%s, %s)" % (self, x))
@classmethod
def class_foo(cls, x):
print("executing class_foo(%s, %s)" % (cls, x))
@staticmethod
def static_foo(x):
print("executing static_foo(%s)" % x)
a = A()
a.foo(1)
A.class_foo(1)
A.static_foo(1)
a.static_foo(1)
</code>The output of the above code is:
<code>executing foo(<__main__.A object at 0x...>, 1)
executing class_foo(<class '__main__.A'>, 1)
executing static_foo(1)
executing static_foo(1)
</code>Underscore naming conventions in Python indicate special meanings: a double leading underscore (e.g., __age ) makes an attribute name‑mangled (private), a single leading underscore (e.g., _age ) signals a protected attribute, and names without underscores are public.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.