In Python, a variable declared within a class but outside any method is referred to as a class or static variable. A class or static variable can be referred to, but not directly through an instance.
Class or static variables are distinct from and do not conflict with other member variables of the same name.
class Student:
stream = 'Science' # Class Variable
def __init__(self,name,roll):
self.name = name # Instance Variable
self.roll = roll # Instance Variable
# Objects of Student class
a = Student('Rohan', 54)
b = Student('Shikha', 23)
print(a.stream)
print(b.stream)
print(a.name)
print(b.name)
print(a.roll)
print(b.roll)
# Class variables can be accessed using class
# name also
print(Student.stream)
# Now if we change the stream for just a it won't be changed for b
a.stream = 'Commerce'
print(a.stream)
print(b.stream)
# To change the stream for all instances of the class we can change it
# directly from the class
Student.stream = 'Arts'
print(a.stream)
print(b.stream)
Science
Science
Rohan
Shikha
54
23
Science
Commerce
Science
Commerce
Arts
Python includes a class-specific static method. It functions similarly to a static variable that is bound to the class rather than the class’s object. A static method can be called without creating an object for the class. This means we can use the class name to directly call the static method. Furthermore, because a static method is bound to a class, it cannot change the state of an object.
There are two ways to define a static method in Python:
@staticmethod
class Animal:
@staticmethod
def info(msg):
# show custom message
print(msg)
print("This class is used for representing different Animal.")
Animal.info("Entering to Animal class")
Entering to Animal class
This class is used for representing different Animal.
@staticmethod
is a more modern approach to defining static method
staticmethod()
class Animal:
def info(msg):
# show custom message
print(msg)
print("This class is used for representing different Animal.")
Animal.info = staticmethod(Animal.info)
Animal.info("Entering to Animal class")
Entering to Animal class
This class is used for representing different Animal.
Here, the info
method is declared as a static method outside the class using the staticmethod()
function approach.
Note: also read about Polymorphism in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…