Python Properties and Class Methods

Learn about Python Class Attributes, Class and Static Methods as well as @property decorator

Photo by Zdeněk Macháček on Unsplash

Class and Static Attributes

class MyClass:
class_var = "class variable"
def __init__(self, instance_var):
self.instance_var = instance_var
def display(self):
print("This is class variable :" + MyClass.class_var)
print("This is instance variable :" + self.instance_var)

==================================>
>>> var = MyClass("instance variable")
>>> var.display()
This is class variable :class variable
This is instance variable :instance variable

Class and Static Method

class MyClass:
def instance_method(self):
return 'instance method called', self
@classmethod
def classmethod(cls):
return 'class method called', cls
@staticmethod
def staticmethod():
return 'static method called'
>>> var = MyClass()
>>> var.instance_method()
(‘instance method called’, <__main__.MyClass object at 0x10cdd1e10>)
>>> var.classmethod()
(‘class method called’, <class ‘__main__.MyClass’>)
>>> var.staticmethod()
‘static method called’
>> MyClass.instance_method()
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: instance_method() missing 1 required positional argument: ‘self’
>>> MyClass.classmethod()
(‘class method called’, <class ‘__main__.MyClass’>)
>>> MyClass.staticmethod()
‘static method called’
>>> class MyCar:
... def __init__(self, brand):
... self.brand = brand
... @classmethod
... def benz(cls):
... return cls(['benz'])
... @classmethod
... def bmw(cls):
... return cls(['bmw'])
... def __repr__(self):
... return repr('This is my ' + self.brand )
>>> MyCar.benz()
MyCar(['benz'])
>>> MyCar.bmw()
MyCar(['bmw'])

@Property decorator

class Mycar:
def __init__(self, brand=None):
self._brand = brand
# getter
def get_brand(self):
print("Getting brand")
return self._brand

# setter
def set_brand(self, brand):
self._brand = value
print("Setting brand", self._brand)
benz = MyCar
benz.set_brand("benz")
benz.get_brand
===========================
Setting brand benz
Getting brand
property(fget=None, fset=None, fdel=None, doc=None)
class Mycar:
def __init__(self, brand=None):
self._brand = brand
# getter
def get_brand(self):
print("Getting brand")
return self._brand

# setter
def set_brand(self, brand):
self._brand = value
print("Setting brand", self._brand)
brand = property(get_brand, set_brand)benz = MyCar
benz.brand = "benz" # NEW SYNTAX
benz.brand # NEW SYNTAX
===========================
Setting brand benz
Getting brand
class Mycar:
def __init__(self, brand=None):
self._brand = brand
@property
def brand(self):
print("Getting brand")
return self._brand

@brand.setter
def brand(self, value):
self._brand = value
print("Setting brand", self._brand)
@brand.deleter
def brand(self):
print("Deleting brand", self._brand)
del self._brand
benz = MyCar
benz.brand = "benz"
benz.brand
===========================
Setting brand benz
Getting brand
class C:
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store