Home » Python

Class or Static variables in Python

Python Class or Static Variables: In this tutorials, we are going to learn about the class or static variables with examples in Python programming language.
Submitted by Bipin Kumar, on December 06, 2019

Python Class / Static Variables

Class or Static variables are class-related variables that are shared among all objects but the instance or non-static variable is unique for each object. In Python, there is no need for the keyword static to make a class variable but the static keyword is used to make a class or static variables in other programming languages like C++, C, Java, etc. The variable defined on the class level that is declared inside the class but outside any method in Python is static.

Let's look at an example for a better understanding of the static variable that returns some details about the intern of "includehelp" in Python.

Example for the class or static variable

# class definition
class intern:
    site_name='Includehelp'
    field='Technical content writer'
    
    def __init__(self,name,programming_language):
        self.name=name
        self.programming_language=programming_language

# object of the intern class
x = intern('Bipin Kumar','Python') 

# printing the variables values
print('Site name: ',x.site_name)
print('Field of interest: ',x.field)
print('Name of intern: ',x.name)
print('Language: ',x.programming_language)

Output

Site name:  Includehelp
Field of interest:  Technical content writer
Name of intern:  Bipin Kumar
Language:  Python

Explanation:

In the above example, site_name and field is a class or static variable which is declared inside the class "intern" but outside of the __init__ method and name and programming_language are instance variables. Here, x is the object of the class "intern" which has two arguments, one is the name of an intern and another one is the programming language.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.