Python is an object-oriented programming language that allows programmers to define objects which can contain data. Python variables let you store particular values in a program.
When you’re working with objects in Python, there are two types of variables you may encounter—instance variables and class variables. But what do these types of variables mean, and how do they work?
That’s the question we’re going to answer in this guide. This tutorial will explore, with examples, the basics of class and instance variables, and how they are used within Python objects.
Python Class Variables
A Python class variable is shared by all object instances of a class. Class variables are declared when a class is being constructed. They are not defined inside any methods of a class.
Because a class variable is shared by instances of a class, the Python class owns the variable. As a result, all instances of the class will be able to access that variable. Class variables are shared by all instances that access the class.
Here is an example of a class variable in Python:
class Espresso: menu_type = "Drink"
In this example, we declare a class variable called menu_type. This class variable is assigned to the class Espresso.
Class variables are useful because they allow you to declare a variable when a class has been built, which can then be used later in your class.
Accessing a Class Variable in Python
Now that we’ve declared a class variable, we can access it when we create an object of our class. So, if we want to create a new class instance and see the value of the menu_type variable, we could use this code:
class Espresso: menu_type = "Drink" espresso_order = Espresso() print(espresso_order.menu_type)
Our code returns: Drink.
In our code, we first define a class that has one class variable: menu_type. Then, we create an instance of our class. This instance is called espresso_order.
In order to see the value of the menu_type variable in our class, we use the dot notation. This is the name of the class followed by a period. Then, we specify the name of the class variable you want to read. This causes our program to return Drink.
Because our class variable is associated with a class, we don’t even need to declare an instance of our class to see its value. The following code allows us to see the value of our menu_type class variable:
class Espresso: menu_type = "Drink" print(Espresso.menu_type)
Our code returns: Drink. In this example, we use the dot notation to access the value of the menu_type variable in our Espresso class. Unlike our previous example, we do not declare an instance of our class.
Changing a Class Variable
Class variables can also be changed, just like any other type of variable. To do so, you can use this code:
class Espresso: menu_type = "Drink" espresso_order = Espresso() espresso_order.menu_type = "Coffee" print(espresso_order.menu_type)
Our code returns: Coffee. In this example, we have declared an instance of our class called espresso_order. Then, we assign the value of the espresso_order.menu_type class variable to be equal to Coffee. This changes the value of the variable.
We print out the new value of the menu_type variable to the console using a Python print statement.
Python Instance Variables
Python instance variables are owned by an instance of a class. The value of an instance variable can be different depending on the instance with which the variable is associated.
This means that the value of each instance variable can be. This is unlike a class variable where the variable can only have one value that you assign. Instance variables are declared inside a class method.
Here is an example of two instance variables in Python:
class CoffeeOrder: def __init__(self, coffee_name, price): self.coffee_name = coffee_name self.price = price
In this example, coffee_name and price are instance variables that exist within our class.
Assinging Values to an Instance Variable in Python
We can assign values to an instance variable when we declare a class. We do this by specifying the values we want to assign as arguments when we declare the class. Suppose we want to create an instance of our class with the following values:
- coffee_name = “Espresso”
- price = 2.10
We could create this instance using the following code:
class CoffeeOrder: def __init__(self, coffee_name, price): self.coffee_name = coffee_name self.price = price customer_order = CoffeeOrder("Espresso", 2.10) print(customer_order.coffee_name) print(customer_order.price)
Our code returns:
Let’s break down our code. First, we declare a class called CoffeeOrder. This class has two instance variables: coffee_name and price.
We create a new instance of our class called customer_order. Then, we assign the value of the coffee_name variable to Espresso, and the value of price to 2.10. On the next line of code, we print out the value of the coffee_name variable using the dot notation. Then, we print out the value of the price variable.
The values stored in our class are those that we passed when we declared our variable customer_order. This variable is an instance of our class.
Using Variables with Multiple Instances of a Python Class
To better distinguish class and instance variables, consider the following example of a Python class constructor (also known as the init method):
class CoffeeOrder: def __init__(self, coffee_name, price): self.coffee_name = coffee_name self.price = price lucas_order = CoffeeOrder("Espresso", 2.10) print(lucas_order.coffee_name) print(lucas_order.price) paulina_order = CoffeeOrder("Latte", 2.75) print(paulina_order.coffee_name) print(paulina_order.price)
Our code returns:
Espresso 2.10 Latte 2.75
In this example, we have defined two instances of our class: lucas_order and paulina_order. Each of these instances has their own values set for the coffee_name and price instance variables.
As you can see, when we print Lucas’ order details to the console, the values Espresso and 2.10 are returned. When we print out Paulina’s order details to the console, the values Latte and 2.75 are returned.
This shows one of the main differences between instance and class variables in action: instance variables can have different values for each instance of the class, whereas class variables are the same across all instances.
Python Class Variables vs. Instance Variables.
Python class variables are declared within a class and their values are the same across all instances of a class. Python instance variables can have different values across multiple instances of a class.
Class variables share the same value among all instances of the class. The value of instance variables can differ across each instance of a class.
Class variables can only be assigned when a class has been defined. Instance variables, on the other hand, can be assigned or changed at any time.
Both class variables and instance variables store a value in a program, just like any other Python variable.
To learn more about Python, read our complete How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more