Python: Decorators in OOP
Tags: programming, java, objectoriented, python
Published: 2021-12-28 05:24:04

---
title: Python: Decorators in OOP
published: true
date: 2021-01-27 13:14:53 UTC
tags: programming,java,objectoriented,python
canonical_url: https://towardsdatascience.com/python-decorators-in-oop-3189c526ead6
---

#### A guide on classmethods, staticmethods and the property decorator

![](https://cdn-images-1.medium.com/max/1024/1*G6JSLU01uhka9uJ2okEcgg.png)<figcaption>Image by author</figcaption>

**The Object Oriented Programming paradigm** became popular in the ’60s and ‘70s, in languages like Lisp and Smalltalk. Such features were also added to existing languages like Ada, Fortran and Pascal.

Python is an object oriented programming language, though it doesn’t support strong encapsulation.

Introductory topics in object-oriented programming in Python — and more generally — include things like defining classes, creating objects, instance variables, the basics of inheritance, and maybe even some special methods like \_\_str\_\_. But when we have an advanced look, we could talk about things like the use of decorators, or writing a custom new method, metaclasses, and Multiple Inheritance.

In this post, we’ll first discuss what decorators are, followed by a discussion on classmethods and staticmethods along with the property decorator.

> Classmethods, staticmethods and property are examples of what are called descriptors. These are objects which implement the \_\_get\_\_ , \_\_set\_\_ or \_\_delete\_\_ methods.

But, that’s a topic for another post.

### Table of Contents

We’ll talk about the following in this article:

```
**- what are decorators?  
- classmethods  
- staticmethods  
- @property**  
```

### An example

Let’s work on a simple example: a Student class.

For now, this class has two variables:

- name
- age
- score

We’ll add a simple \_\_init\_\_ method to instantiate an object when these two attributes are provided.

{% gist https://gist.github.com/Polaris000/182b5952c4755a85102f2910482df4c0 %}

We’ll modify this as we go throughout the post.

### Decorators

> Decorators are functions (or classes) that provide enhanced functionality to the original function (or class) without the programmer having to modify their structure.

A simple example?

Suppose we want to add a method to our Student class that takes a student’s score and total marks and then returns a percentage.

![](https://cdn-images-1.medium.com/max/1024/1*jpUxr9kXGK0nnHcUMFkVMQ.png)<figcaption>The get_percent function — Image by author using <a href="https://app.diagrams.net/">draw.io</a></figcaption>

Our percent function can be defined like so:

{% gist https://gist.github.com/Polaris000/41d96ba28d2640e8519bd268cd28fe56 %}

Let’s define our decorator, creatively named record\_decorator. It takes a function as input and outputs another function ( wrapper , in this case).

![](https://cdn-images-1.medium.com/max/1024/1*kX0alKprN_8ICsC-hrQfEw.png)<figcaption>Our decorator — Image by author using <a href="https://app.diagrams.net/">draw.io</a></figcaption>

The wrapper function:

- takes our two arguments score and total
- calls the function object passed to the grade\_decorator
- then calculates the grade that corresponding to the percent scored.
- Finally, it returns the calculated percentage along with the grade.

![](https://cdn-images-1.medium.com/max/1024/1*_0KivLuoTGOw2_YQyX3EUw.png)<figcaption>How applying the decorator works — Image by author using <a href="https://app.diagrams.net/">draw.io</a></figcaption>

We can implement our decorator like so.

{% gist https://gist.github.com/Polaris000/027d6d16e9cf2ff3b738b3ff401b9763 %}

Now, to improve the get\_percent function, just use the @ symbol with the decorator name above our function, which has exactly the same definition as before.

{% gist https://gist.github.com/Polaris000/4f4e63da4819cb6eca1dacd1638212f9 %}

To use this, we don’t need to modify our call statement. Executing this:

```
**get\_percent(25, 100)**
```

returns

```
**25.0, D**
```

What basically happens is that the function get\_percent is replaced by wrapper when we apply the decorator.

We’ll place the get\_percent method inside the Student class, and place our decorator outside the class. Since get\_percent is an instance method, we add a self argument to it.

{% gist https://gist.github.com/Polaris000/6eca8e7765b07d2d2abbac0d0056b6b8 %}

How are decorators used in classes?

We’ll see three popular decorators used in classes and their use-cases.

![](https://cdn-images-1.medium.com/max/1024/1*TaqEDwhHTSZS9-DrLXe8uA.png)<figcaption>The kinds of methods in our class A — Image by author using <a href="https://app.diagrams.net/">draw.io</a></figcaption>

### classmethod

Let’s first talk about instance methods. Instance methods are those methods that are called by an object, and hence are passed information about that object. This is done through the self argument as a convention, and when that method is called, the object’s information is passed implicitly through self.

For example, we could add a method to our class that calculates a student’s grade and percentage (using the get\_percent method) and generates a report as a string with the student’s name, percentage, and grade.

{% gist https://gist.github.com/Polaris000/20e33e09952ac8c685f9bfc01d185577 %}

**Coming to a class method** , this type of function is called on a class, and hence, it requires a class to be passed to it. This is done with the cls argument by convention. And we also add the @classmethod decorator to our class method.

It looks something like this:

```
class A:
    def instance_method(self):
        return self

 **@classmethod  
 def class\_method(cls):  
 return cls**  

A.class_method()
```

#### use-cases for class-methods

Since class-methods work with a class, and not an instance, they can be used as part of a **factory pattern,** where objects are returned based on certain parameters.

For example, there are multiple ways to create a Dataframe in pandas. There are methods like: from\_records() , from\_dict() , etc. which all return a dataframe object. Though their actual implementation is pretty complex, they basically take something like a dictionary, manipulate that and then return a dataframe object after parsing that data.

Coming back to our example, let's define a few ways to create instances of our Student class.

- by two separate arguments: for example, <name>, 20 and 85
- by a comma-separated string: for example, “<name>, 20, 85”.
- by a tuple: for example, (<name>, 20, 85)

To accomplish this in Java, we could simply overload our constructor:

{% gist https://gist.github.com/Polaris000/e0a03e0894795a86ba7fb57a5d001259 %}

In python, a clean way to do it would be through classmethods:

{% gist https://gist.github.com/Polaris000/ab68493868af8ea08dd873c76d469a69 %}

We also define the \_\_str\_\_ method, so we can directly print a Student object to see if it has been instantiated properly. Our class now looks like this:

{% gist https://gist.github.com/Polaris000/3546ee7f96f6ca3f5ece0432a32b50b0 %}

Now, to test this, let’s create three Student objects, each from a different kind of data.

{% gist https://gist.github.com/Polaris000/69127ffd9c5fbbbc4af7efc421028b3c %}

The output is exactly as expected from the definition of the \_\_str\_\_ method above:

```
Name: John Score: 25 Total : 100
Name: Jack Score: 60 Total : 100
Name: Jill Score: 125 Total : 200
```

### staticmethod

A static method doesn’t care about an instance, like an instance method. It doesn’t require a class being passed to it implicitly.

A static method is a regular method, placed inside a class. It can be called using both a class and an object. We use the @staticmethod decorator for these kinds of methods.

A simple example:

```
class A:
    def instance_method(self):
        return self

    @classmethod
    def class_method(cls):
        return cls

 **@staticmethod  
 def static\_method():  
 return**  

a = A()

a.static_method()
A.static_method()
```

#### use-cases for the staticmethod

Why would this be useful? Why not just place such functions outside the class?

Static methods are used instead of regular functions when it makes more sense to place the function inside the class. For example, placing utility methods that deal solely with a class or its objects is a good idea, since those methods won’t be used by anyone else.

Coming to our example, we can make our get\_percent method static, since it serves a general purpose and need not be bound to our objects. To do this, we can simply add @staticmethod above the get\_percent method.

{% gist https://gist.github.com/Polaris000/f0046ddf138e5fa2499209d3a46137e8 %}

### property

The property decorator provides methods for accessing (_getter_), modifying (_setter_), and deleting (_deleter_) the attributes of an object.

![](https://cdn-images-1.medium.com/max/1024/1*9oOfB4c_Wqvnde8SOgdiqQ.png)<figcaption>The property decorator — Image by author using <a href="https://app.diagrams.net/">draw.io</a></figcaption>

#### The getter and setter methods

Let’s start with getter and setter methods. These methods are used to access and modify (respectively) a private instance. In Java, we would do something like this:

{% gist https://gist.github.com/Polaris000/2ba3b9bf176559f03bfe77d6df76d05f %}

Now, anytime you access or modify this value, you would use these methods. Since the variable x is private, it can’t be accessed outside JavaClass .

{% gist https://gist.github.com/Polaris000/960b9288836bdecf2f4a8f927967ffee %}

> In python, there is no private keyword. We prepend a variable by a dunder(\_\_ ) to show that it is private and shouldn’t be accessed or modified directly.

Adding a \_\_ before a variable name modifies that variable’s name from varname to \_Classname\_\_varname , so direct access and modification like print(obj.varname) and obj.varname = 5 won’t work. Still, this isn’t very strong since you could directly replace varname with the modified form to get a direct modification to work.

Let’s take the following example to understand this:

#### **Adding getter and setter methods**

Taking our Student class example, let’s make the score attribute “private” by adding a \_\_ before the variable name.

If we directly went ahead and added get\_score and set\_score like Java, the main issue is that if we wanted to do this to existing code, we’d have to change every access from:

```
**print("Score: " + str(student1.score))**
 **student1.score = 100**
```

to this:

```
**print(student1.get\_score())  
student1.set\_score(100)**  
```

Here’s where the @property decorator comes in. You can simply define getter, setter and deleter methods using this feature.

{% gist https://gist.github.com/Polaris000/ea1d708e87cf11b2cd0ad42a28560a21 %}

Our class now looks like this:

{% gist https://gist.github.com/Polaris000/af7adaada1d2fc35a4bf1098e5b3dbb0 %}

**To make the attribute**  **score read-only,** just remove the setter method.

Then, when we update score, we get the following error:

```
Traceback (most recent call last):
  File "main.py", line 16, in <module>
    student.score = 10
**AttributeError: can't set attribute**
```

#### **The deleter method**

The deleter method lets you delete a protected or private attribute using the del function. Using the same example as before, if we directly try and delete the score attribute, we get the following error:

```
student = Student("Tom", 50, 100)
del student.score

This gives:
Traceback (most recent call last):
  File "<string>", line 17, in <module>
**AttributeError: can't delete attribute**
```

But when we add a deleter method, we can delete our private variable score .

{% gist https://gist.github.com/Polaris000/f279e158fc49f6c227cca87799013f52 %}

The attribute has been successfully removed now. Printing out the value of score gives “object has no attribute…”, since we deleted it.

```
Traceback (most recent call last):
  File "<string>", line 23, in <module>
File "<string>", line 9, in x
**AttributeError: 'PythonClass' object has no attribute '\_\_score'**
```

#### use-cases for the property decorator

The property decorator is very useful **when defining methods for data validation** , like when deciding if a value to be assigned is valid and won’t lead to issues later in the code.

Another use-case would be when **wanting to display information in a specific way**. Coming back to our example, if we wanted to display a student’s name as “Student Name: <name>” , instead of just <name> , we could return the first string from a property getter on the name attribute:

{% gist https://gist.github.com/Polaris000/139c8ced748022c654ec07f9ef181c8e %}

Now, any time we access name, we get a formatted result.

```
student = Student("Bob", 350, 500)
**print(student.name)**
```

The output:

```
**Student Name: Bob**
```

**The**  **property decorator can also be used for logging changes.**

For example, in a setter method, you could add code to log the updating of a variable.

{% gist https://gist.github.com/Polaris000/85423444687d5c432d6994b6a4f60ea4 %}

Now, whenever the setter is called, which is when the variable is modified, the change is logged. Let’s say there was a totaling error in Bob’s math exam and he ends up getting 50 more marks.

```
student = Student("Bob", 350, 500)
print(student.score)  
**student.score = 400** print(student.score)
```

The above gives the following output, with the logged change visible:

```
70.0 %
**INFO:root:Setting new value...**
80.0 %
```

Finally, our class looks like this:

{% gist https://gist.github.com/Polaris000/9775941c4d0b36790c078dcf4bece037 %}

#### Note#1: Where should you define a decorator wrt a class?

There are many places you could define a decorator: outside the class, in a separate class, or maybe even in an inner class (with respect to the class we are using the decorator in). In this example, we simply defined grade\_decorator outside the Student class. Though this works, the decorator now has nothing to do with our class, which we may not prefer.

For a more detailed discussion on this, check out this post:

[Decorator inside Python class](https://medium.com/@vadimpushtaev/decorator-inside-python-class-1e74d23107f6)

#### Note#2: Are there options other than constructor overloading in Java to simulate the methods we discussed (like, from\_str or from\_tuple)?

Apart from overloading the constructor, we could make use of **static factory methods in java**. We could define a static method like from\_str that would extract key information from the string passed to it and then return an object.

### Conclusion

Object-oriented programming is a very important paradigm to learn and use. Regardless of whether you’ll ever need to use the topics discussed here in your next project, it’s necessary to know the basics really well. Topics like the ones in this post aren’t used all that often compared to more basic concepts — like inheritance or the basic implementation of classes and objects — on which they are built. In any case, I hope this post gave you an idea of the other kinds of methods in Python OOP (apart from instance methods) and the property decorator.

* * *