class variable in Python
Introduction
In object-oriented programming with classes, a class variable is any variable
declared with a static modifier for which a single copy exists, regardless of
how many instances of the class exist.
A class variable is not an instance variable. It is a special type of class attribute
(or class property, field or data member).
The same dichotomy between an instance and class members also applies to methods ("member functions");
a class can have both instance methods and class methods.
Example
class Site(): protocol = "https" pass hh = Site() hh.domain = "www.heihei.ru" hh.name = "HeiHei.ru" tb = Site() tb.domain = "www.heihei.ru" tb.name = "TopBicycle.ur" print(hh.protocol, hh.domain, tb.protocol, tb.domain)
python class_variable.py
https www.heihei.ru https www.heihei.ru
Example with __init__()
Before studying the next example I recommend reading the articles about __init__() and self
class Cosmo: cosmo_count = 0 raise_amount = 1.04 def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay self.fullname = first + " " + last self.email = first + '.' + last + '@vostok.su' Cosmo.cosmo_count += 1 def apply_raise(self): self.pay = int(self.pay * self.raise_amount) # also possible # self.pay = int(self.pay * Cosmo.raise_amount) # but not # self.pay = int(self.pay * raise_amount) // NameError: name 'raise_amount' is not defined # Class print(Cosmo.__dict__) # Create two instances cosm_2 = Cosmo('German', 'Titov', 2000) cosm_3 = Cosmo('Andriyan', 'Nikolaev', 3000) # Check cosm_2 instance print(cosm_2.__dict__) # Check how raise() works print(f"Initial {cosm_2.last} pay: {cosm_2.pay}") cosm_2.apply_raise() print(f"New {cosm_2.last} pay: {cosm_2.pay}") print(cosm_2.__dict__) # Check that raise amount changed for one instance # does not affect other instances print(f"Initial {cosm_2.last} raise_amount: {cosm_2.raise_amount}") cosm_2.raise_amount = 1.1 print(cosm_2.__dict__) print(f"New {cosm_2.last} raise_amount: {cosm_2.raise_amount}") print(f"{cosm_3.last} raise_amount remains: {cosm_3.raise_amount}") # Check that instance count works print(f"Created {Cosmo.cosmo_count} cosmonaut profiles")
python class_variable.py
{'__module__': '__main__', 'cosmo_count': 2, 'raise_amount': 1.04, '__init__': <function Cosmo.__init__ at 0x7fa3f5e5ea60>, 'apply_raise': <function Cosmo.apply_raise at 0x7fa3f5e5eaf0>, '__dict__': <attribute '__dict__' of 'Cosmo' objects>, '__weakref__': <attribute '__weakref__' of 'Cosmo' objects>, '__doc__': None} {'first': 'German', 'last': 'Titov', 'pay': 2000, 'fullname': 'German Titov', 'email': 'German.Titov@vostok.su'} Initial Titov pay: 2000 New Titov pay: 2080 {'first': 'German', 'last': 'Titov', 'pay': 2080, 'fullname': 'German Titov', 'email': 'German.Titov@vostok.su'} Initial Titov raise_amount: 1.04 {'first': 'German', 'last': 'Titov', 'pay': 2080, 'fullname': 'German Titov', 'email': 'German.Titov@vostok.su', 'raise_amount': 1.1} New Titov raise_amount: 1.1 Nikolaev raise_amount remains: 1.04 Created 2 cosmonaut profiles
Article author: Andrei Olegovich