Python Properties and Class Methods

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

Image for post
Image for post
Photo by Zdeněk Macháček on Unsplash

If you are like me who have a few months experience of Python, then you might be in the same position as me — know something about Python, but not enough. So I spent sometime to gather bits and pieces of scattered knowledge to dig deeper into Python. In this blog, the piece will be the Python Class Attributes, Class and Static Methods as well as @property decorator.

First, let’s look at the class attribute— both exist on the class itself but not on the instance of class.

You can see class_var only exist on the class MyClass. This concept originates from C++ and C, which uses static keywords to make a variable a class variable.

Now let’s move on to the class and static method. As convenience, we will compare them with the common instance methods:

Instance Methods: The first methodmethod is an instance method. The parameter selfpoints to an instance of MyClass. It can also access the class itself through self.__class__ property.

Class Methods: The classmethod with the @classmethod decorator is a class method. It takes a cls parameter that points to the class, and can only modify class states.

Static Methods: The staticmethod with@staticmethod decorator is a static method. It does not take the mandatory self nor a cls parameter. As a result, it can’t access class or instance state

Note that we can also call the later two methods on the class directly (As expected) but not on the instance method:

When choosing between`@staticmethod and @classmethod , think about the latter requires access to the class object to call other class methods, while the former has no access needed to either class or instance objects. It can be moved up to the module-scope.

On the use cases, one example with @classmethod is to have the flexibility to use it as factory functions to initiate variants of class instances without using inheritance. It is also a good practice to communicate your design intent with others:

Now to make car with certain brand.

As a result, we use the factory functions to create new MyCar objects that are configured with certain brand, which all use the same __init__ constructor and provide a shortcut to create cars with different brands.

And finally, the @property decorator. Simply put, the property() method provides an interface to instance attributes, aka, getter , setter and deleter .

Why that is needed?

Well, it is common to allow dynamically setting and getting an instance attribute when you build a class, e.g. the brand attribute below:

This can do, but it’s a bit tedious. Also what if the other developer comes in and wants to have another naming convention like instead of calling set_brand and get_brand , it will be setting_brand and getting_brand ?

We can use property() method in Python — a built-in function that creates and returns a property object, to solve this problem:

  • fget is function to get value of the attribute
  • fset is function to set value of the attribute
  • fdel is function to delete the attribute
  • doc is a docstring

So now we can have:

As you might have sensed, the property can be implemented as a decorator to your instance method:

Above example can be generalised into below pattern:

So that’s it for today!

Happy Reading!

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