Class or Static Variables in Python - GeeksforGeeks

03 Aug, 2021
All objects share class or static variables. An instance or non-static variables are different for different objects (every object has a copy). For example, let a Computer Science Student be represented by class CSStudent. The class may have a static variable whose value is “cse” for all objects. And class may also have non-static members like name and roll. In C++ and Java, we can use static keywords to make a variable a class variable. The variables which don’t have a preceding static keyword are instance variables. See this for the Java example and this for the C++ example.The Python approach is simple; it doesn’t require a static keyword. 
All variables which are assigned a value in the class declaration are class variables. And variables that are assigned values inside methods are instance variables. 
Python
# Python program to show that the variables with a value# assigned in class declaration, are class variables # Class for Computer Science Studentclass CSStudent:    stream = 'cse'                  # Class Variable    def __init__(self,name,roll):        self.name = name            # Instance Variable        self.roll = roll            # Instance Variable # Objects of CSStudent classa = CSStudent('Geek', 1)b = CSStudent('Nerd', 2) print(a.stream)  # prints "cse"print(b.stream)  # prints "cse"print(a.name)    # prints "Geek"print(b.name)    # prints "Nerd"print(a.roll)    # prints "1"print(b.roll)    # prints "2" # Class variables can be accessed using class# name alsoprint(CSStudent.stream) # prints "cse" # Now if we change the stream for just a it won't be changed for ba.stream = 'ece'print(a.stream) # prints 'ece'print(b.stream) # prints 'cse' # To change the stream for all instances of the class we can change it# directly from the classCSStudent.stream = 'mech' print(a.stream) # prints 'ece'print(b.stream) # prints 'mech'
Output: 
cse
cse
Geek
Nerd
1
2
cse
ece
cse
ece
mech
YouTubeGeeksforGeeks501K subscribersPython Programming Tutorial | Class or Static Variables in Python | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:13•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=FlGiKthOFbU" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Harshit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.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
rahulpy
adarshb20
neeruto1
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()