In the previous fact, we have seen that Python doesn’t have static keyword. All variables that are assigned a value in class declaration are class variables
We should be careful when changing value of class variable. If we try to change class variable using object, a new instance (or non-static) variable for that particular object is created and this variable shadows the class variables. Below is Python program to demonstrate the same.
Initially a.stream = cse b.stream = cse After changing a.stream a.stream = ece b.stream = cse
We should change class variables using class name only.
a.tream = cse Class variable changes to mec Value of variable steam for each object a.stream = mec b.stream = mec
This article is contributed by Nikhil Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- self in Python class
- First Class functions in Python
- Class as decorator in python
- Class or Static Variables in Python
- Class & Instance Attributes in Python
- String Template Class in Python
- Python | Using variable outside and inside the class and method
- Python | Avoiding class data shared among the instances
- CBSE Class 11 | Computer Science - Python Syllabus
- CBSE Class 12 | Computer Science - Python Syllabus
- Python Tkinter | Create different type of lines using Canvas class
- class method vs static method in Python
- CBSE Class 11 C++ | Sample Paper-1
- Computer Science Class XII (2016-17)