Last updated: Apr 9, 2024
Reading timeยท5 min
Update class variables by accessing them directly on the class.
Class variables are shared by all instances and can be updated directly on the class or in a class method.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary # โ update class variable @classmethod def set_cls_id(cls, new_cls_id): cls.cls_id = new_cls_id return cls.cls_id # โ update instance variable def set_name(self, new_name): self.name = new_name return new_name print(Employee.cls_id) # ๐๏ธ employee Employee.set_cls_id('new_employee_id') print(Employee.cls_id) # ๐๏ธ new_employee_id bob = Employee('Bobbyhadz', 100) bob.set_name('Bobbyhadz2') print(bob.name) # ๐๏ธ Bobbyhadz2
The class has a cls_id
class variable and name
and salary
instance
variables.
Employee.cls_id
.Instance variables are unique to each instance you create by instantiating the class.
We used a class method to update the cls_id
variable.
@classmethod def set_cls_id(cls, new_cls_id): cls.cls_id = new_cls_id return cls.cls_id
Class methods get passed the class as the first argument.
Note that you can also set class variables directly on the class, e.g.
Employee.cls_id = new_cls_id
.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary print(Employee.cls_id) # ๐๏ธ employee # โ Update a class variable Employee.cls_id = 'new_employee_id' print(Employee.cls_id) # ๐๏ธ new_employee_id
Class variables are shared by all instances of the class, whereas instance variables are unique to each instance.
You'll more often have to update instance variables in the class. Here is an example.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary # โ Update an instance variable def set_name(self, new_name): self.name = new_name return new_name alice = Employee('Alice', 150) bob = Employee('Bobbyhadz', 100) bob.set_name('Bobbyhadz2') print(bob.name) # ๐๏ธ Bobbyhadz2 print(alice.name) # ๐๏ธ Alice
Updating one instance variable doesn't update the attribute for the other instances.
On the other hand, when you update a class variable, the value is updated for all instances.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary alice = Employee('Alice', 150) bob = Employee('Bobbyhadz', 100) print(bob.cls_id) # ๐๏ธ employee Employee.cls_id = 'NEW_ID' print(alice.cls_id) # ๐๏ธ NEW_ID print(bob.cls_id) # ๐๏ธ NEW_ID
Updating the cls_id
class variable is reflected in all instances.
You can use the type()
class if you need to access a class variable from an
instance of the class.
class Employee(): cls_id = 'employee' def __init__(self, name, salary): self.name = name self.salary = salary bob = Employee('Bobbyhadz', 100) # ๐๏ธ override class variable on the instance bob.cls_id = 'new' print(bob.cls_id) # ๐๏ธ new # ๐๏ธ access the actual class variable from the instance result = type(bob).cls_id print(result) # ๐๏ธ employee
The instance overrides the cls_id
variable, so to access the actual class
variable, we had to use the type()
class.
The type class returns the type of an object.
__class__
attribute on the object.The following code sample uses the __class__
attribute and achieves the same
result.
bob = Employee('Bobbyhadz', 100) bob.cls_id = 'new' print(bob.cls_id) # ๐๏ธ new result = bob.__class__.cls_id print(result) # ๐๏ธ employee
Class variables are accessed directly on the class.
You can access instance variables using the
self object, e.g. self.name
. Instance
variables are unique to each instance.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary def get_name(self): # ๐๏ธ class variable print(Employee.cls_id) # ๐๏ธ access instance variable return self.name bob = Employee('Bobbyhadz', 100) print(bob.get_name()) # ๐๏ธ Bobbyhadz print(bob.cls_id) # ๐๏ธ employee print(Employee.cls_id) # ๐๏ธ employee
The class has a cls_id
class variable and name
and salary
instance
variables.
Employee.cls_id
.Instance variables are unique to each instance you create by instantiating the class.
You can also access class variables by defining a class method.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary @classmethod def get_cls_id(cls): return cls.cls_id print(Employee.get_cls_id()) # ๐๏ธ employee
We marked the get_cls_id
method as a class method. The first argument class
methods get passed is the class.
Regular methods get passed a self
object that gives us a reference to the
instance.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary def get_name(self): print(Employee.cls_id) return self.name alice = Employee('Alice', 200) print(alice.get_name()) # ๐๏ธ Alice print(alice.cls_id) # ๐๏ธ employee bob = Employee('Bobbyhadz', 100) print(bob.get_name()) # ๐๏ธ Bobbyhadz print(bob.cls_id) # ๐๏ธ bob
The name
and salary
instance variables are unique to each instance, but the
instances share the same cls_id
class variable.
You can use the type()
class if you need to access a class variable from an
instance of the class.
class Employee(): # ๐๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐๏ธ instance variables self.name = name self.salary = salary def get_name(self): # ๐๏ธ class class variable print(Employee.cls_id) # ๐๏ธ access instance variable return self.name bob = Employee('Bobbyhadz', 100) # ๐๏ธ override class variable on the instance bob.cls_id = 'new' print(bob.cls_id) # ๐๏ธ new # ๐๏ธ access the actual class variable from the instance result = type(bob).cls_id print(result) # ๐๏ธ employee
The instance overrides the cls_id
variable, so to access the actual class
variable, we had to use the type()
class.
The type class returns the type of an object.
__class__
attribute on the object.The following code snippet uses the __class__
attribute and achieves the same
result.
bob = Employee('Bobbyhadz', 100) bob.cls_id = 'new' print(bob.cls_id) # ๐๏ธ new result = bob.__class__.cls_id print(result) # ๐๏ธ employee
Which approach you pick is a matter of personal preference.
The type()
class is more elegant because we don't have to access attributes
prefixed with two underscores, but the __class__
attribute is more explicit.
You can learn more about the related topics by checking out the following tutorials: