7 Class and Instance Variables
7 Class and Instance Variables
In this lesson, we'll learn about the concepts of instance variables and class variables, and how to use class
variables correctly.
• De nitions
• Class Variables
• Instance Variables
• De ning Class Variables and Instance Variables
• Wrong Use of Class Variables
• Using Class Variables Smartly
• Explanation
class variables
instance variables
Properties
class instance
variables variables
De nitions #
Class Variables #
The class variables are shared by all instances or objects of the classes. A
change in the class variable will change the value of that property in all the
objects of the class.
Instance Variables #
The instance variables are unique to each instance or object of the class. A
change in the instance variable will change the value of the property in that
specific object only.
class Player:
teamName = 'Liverpool' # class variables
p1 = Player('Mark')
p2 = Player('Steve')
print("Name:", p1.name)
print("Team Name:", p1.teamName)
print("Name:", p2.name)
print("Team Name:", p2.teamName)
class Player:
formerTeams = [] # class variables
teamName = 'Liverpool'
def __init__(self, name):
p1 = Player('Mark')
p2 = Player('Steve')
p1 = Player('Mark')
p1.formerTeams.append('Barcelona')
p2 = Player('Steve')
p2.formerTeams.append('Chelsea')
print("Name:", p1.name)
print("Team Name:", p1.teamName)
print(p1.formerTeams)
print("Name:", p2.name)
print("Team Name:", p2.teamName)
print(p2.formerTeams)
In the example above, while the instance variable name is unique for each and
every object of the Player class, the class variable, formerTeams , can be
accessed by any object of the class and is updated throughout. We are storing
all players currently playing for the same team, but each player in the team
may have played for different former teams. To avoid this issue, the correct
implementation of the example above will be the following:
class Player:
teamName = 'Liverpool' # class variables
p1 = Player('Mark')
p1.formerTeams.append('Barcelona')
p2 = Player('Steve')
p2.formerTeams.append('Chelsea')
print("Name:", p1.name)
print("Team Name:", p1.teamName)
print(p1.formerTeams)
print("Name:", p2.name)
print("Team Name:", p2.teamName)
print(p2.formerTeams)
Now the property formerTeams is unique for each Player class object and can
only be accessed by that unique object.
class Player:
teamName = 'Liverpool' # class variables
teamMembers = []
p1 = Player('Mark')
p2 = Player('Steve')
print("Name:", p1.name)
print("Team Members:")
print(p1.teamMembers)
print("")
print("Name:", p2.name)
print("Team Members:")
print(p2.teamMembers)
Explanation #
In the example above, we’ve defined a class variable teamMembers , which
is a list that will be shared by all the objects of the class Player .
This list, teamMembers , will contain names of all the instances created of
the Player class.
As you can see in line 8, whenever a new object is created, its name is
appended in teamMembers .
In line 16 and line 20, we can see that teamMembers is accessed by p1 and
p2 respectively and both produce the same output.
In the next lesson, we’ll learn about implementing methods in a class.