Understanding Python 3's Class and Instance Variables
At 6/4/2023
Variables may be utilized at the class level or the instance level in object-oriented programming. In essence, variables are just symbols that represent a value you're utilizing in a program.
Variables are referred to as class variables when they are at the class level and instance variables when they are at the instance level.
We can specify a variable at the class level if we anticipate that it will be constant across instances or if we want to initialize it. We can define variables at the instance level when we expect significant variation between instances.
The DRY principle, which stands for "don't repeat yourself," is one of the guiding principles of software development. This rule aims to eliminate duplication in code, and object-oriented programming follows it because it does so by cutting down on redundancy.
Python object-oriented programming using class and instance variables will be demonstrated in this course.
Class Variables
Class variables are defined withi n the class construction . Because they are owned by the class itself, class variables are shared by all instances of the class. They therefore will generally have the same value for every instance unless you are using the class variable to initialize a variable.
Defined outside of all the methods, class variables are, by convention, typically placed right below the class header and before the constructor method and other methods.
Info:
To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the
python3
command. Then you can copy, paste, or edit the examples by adding them after the
>>>
prompt.
A class variable alone looks like the following:
Here, the variable
animal_type
is assigned the value
"fish"
.
We can create an instance of the
Shark
class (we’ll call it
new_shark
) and print the variable by using dot notation:
Let’s run the program:
Outputfish
Our program returns the value of the variable.
Let’s add a few more class variables and print them out:
Class variables can contain any Python-compatible data type, just like any other variable. An integer and strings are both present in this application. Reviewing the output, let's run the program once more using the Python shark.py command:
Outputfish ocean 5
When we run the program, the instance of new_shark has access to all the class variables and may print them out.
When creating a class, we can define variables using class variables. Each instance of the class can then access these variables and the values that are associated with them.
Instanc e Variables
Instance variables are owned by instances of the class. This means that for each object or instance of a class, the instance variables are different.
Unlike class variables, instance variables are defined within methods.
In the
Shark
class example below,
name
and
age
are instance variables:
When we create a
Shark
object, we will have to define these variables, which are passed as parameters within the constructor method or another method.
As with class variables, we can similarly call to print instance variables:
When we run the program above with
python shark.py
, we’ll receive the following output:
OutputSammy
5
The output we receive is made up of the values of the variables that we initialized for the object instance of
new_shark
.
Let’s create another object of the
Shark
class called
stevie
:
OutputSammy
5
Stevie
8
The
stevie
object, like the
new_shark
object passes the parameters specific for that instance of the
Shark
class to assign values to the instance variables.
Instance variables, owned by objects of the class, allow for each object or instance to have different values assigned to those variables.
Working with Class and Instance Variables Together
Class variables and instance variables will often be utilized at the same time, so let’s look at an example of this using the
Shark
class we created. The comments in the program outline each step of the process.
When we run the program with
python shark.py
, we’ll receive the following output:
OutputSammy
ocean
Stevie
This user has 77 followers
fish
Here, we have made use of both class and instance variables in two objects of the
Shark
class,
sammy
and
stevie
.
Conclustion
In object-oriented programming, class variables are variables at the class level, whereas instance variables are variables at the object level.
This distinction enables us to utilize instance variables to use separate variables for each object and class variables to initialize objects with a specified value set to variables.
Using class- and instance-specific variables can help us follow the DRY principle and prevent redundancy in our code.