Day - 35 of DevOps

Day - 35 of DevOps

Type of Python Class Variables

Object-oriented programming allows for variables to be used at the class level or the instance level.

Variables are essentially symbols that stand in for a value you’re using in a program.

In Python, class variables (also known as class attributes) are shared across all instances (objects) of a class. They belong to the class itself, not to any specific instance.

In Object-oriented programming, we use instance and class variables to design a Class.

In Class, attributes can be defined into two parts:

  • Instance variables: If the value of a variable varies from object to object, then such variables are called instance variables.

  • Class Variables: A class variable is a variable that is declared inside of a Class but outside of any instance method or __init__() method.

    Understand Class Variables

Class Variables

Class variables are defined within 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.

A class variable alone looks like the following:

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:

class Shark:
    animal_type = "fish"

new_shark = Shark()
print(new_shark.animal_type)

Output:

fish

Instance Variables

Instance variables are owned by an instance 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:

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:

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 pythonshark.py, we’ll receive the following

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:

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)

Output:

Sammy
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.

Class variables and Instance variables example

class mobile():
    chargetype="C-Type"
    def __init__(self,brand,price):
        self.brand=brand
        self.price=price
    def display(self):
        print("Brand: ", self.brand)
        print("Price: ", self.price)
        print("Charge type:", self.chargetype)

mobile1=mobile("iPhone",80000)
mobile1.display()

mobile2=mobile("Samsung",70000)
mobile2.display()

Here we used both class and instance variables in the same class(mobile)

used (chargetype) class variable and used self.brand & self.price Instance variables inside the self-constructor function

we are passing values of the instance variables through objects(mobile1 & mobile2)

we return the (display) function we get the output like below, here class variable (chargetype) is commonly listed in both objects, and mobile1 and mobile2 objects values are listed as we passed through objects.

Output:

Brand:  iPhone
Price:  80000
Charge type: C-Type
Brand:  Samsung
Price:  70000
Charge type: C-Type

Congrats🎉, On Learning the Python class variable types with example programs.

Happy Learning :)