- Home
- python Tutorial
- classes
Classes in Python
Introduction
In object oriented we can use the variables at class level or instance level.
The variables at the class level are known as class variables and variables at the level of instance are called as instance variables.
We define a variable as a class variable when the variable is expected to be consistent throughout instances, whereas we define a variable as an instance variable when the variable is expected to be inconsistent.
Object oriented programming aims at reducing the redundancy of code i.e. reducing the repetition of code.
Class Variables
We define class variables in the class construction. These class variables are shared between all the instances in the class.
They are typically defined right below the class header and right above the class constructor.
class Shark:
animal_type = "fish"
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: ######shark.py
class Shark:
animal_type = "fish"
new_shark = Shark()
print(new_shark.animal_type)
Let’s run the program:
python shark.py
fish
Our program returns the value of the variable.
Let’s add a few more class variables and print them out:
shark.py
class Shark:
animal_type = "fish"
location = "ocean"
followers = 5
new_shark = Shark()
print(new_shark.animal_type)
print(new_shark.location)
print(new_shark.followers)
Just like with any other variable, class variables can consist of any data type available to us in Python. In this program we have strings and an integer. Let’s run the program again with the python shark.py
command and see the output:
Output
fish
ocean
5
The instance of new_shark
is able to access all the class variables and print them out when we run the program.
Instance variables are declared and initialized within an instance. The scope of an instance is limited to the instance it is defined in.
class Shark:
def __init__(self, name, age):
self.name = name
self.age = age
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.
class Shark:
def __init__(self, name, age):
self.name = name
self.age = age
new_shark = Shark("Sammy", 5)
As with class variables, we can similarly call to print instance variables:
shark.py
class Shark:
def __init__(self, name, age):
self.name = name
self.age = age
new_shark = Shark("Sammy", 5)
print(new_shark.name)
print(new_shark.age)
When we run the program above with python shark.py
, we’ll receive the following output:
Output
Sammy
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:
shark.py
class Shark:
def __init__(self, name, age):
self.name = name
self.age = age
new_shark = Shark("Sammy", 5)
print(new_shark.name)
print(new_shark.age)
stevie = Shark("Stevie", 8)
print(stevie.name)
print(stevie.age)
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 and instance variables used together
shark.py
class Shark:
# Class variables
animal_type = "fish"
location = "ocean"
# Constructor method with instance variables name and age
def __init__(self, name, age):
self.name = name
self.age = age
# Method with instance variable followers
def set_followers(self, followers):
print("This user has " + str(followers) + " followers")
def main():
# First object, set up instance variables of constructor method
sammy = Shark("Sammy", 5)
# Print out instance variable name
print(sammy.name)
# Print out class variable location
print(sammy.location)
# Second object
stevie = Shark("Stevie", 8)
# Print out instance variable name
print(stevie.name)
# Use set_followers method and pass followers instance variable
stevie.set_followers(77)
# Print out class variable animal_type
print(stevie.animal_type)
if __name__ == "__main__":
main()
When we run the program with python shark.py
, we’ll receive the following output:
Output
Sammy
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
.
Conclusion
In object-oriented programming, variables at the class level are referred to as class variables, whereas variables at the object level are called instance variables.