One of most powerful and advanced features of “Object-Oriented Programming” in python, is “Decorators”. But before you dive in to the decorator’s topic, you need to know somethings in advance. For C++ and Java programmers, functions inside of functions is weird and unfamiliar, but python supports creating functions inside of functions and a function can have an argument which is a function itself! Please don’t mess it up with recursion. In recursion (which is supported by C++ and java as well), same function is called inside of definition of function, but in python you can define a new function inside a function (like a wrapper function), and return of your function could be a function. This is so beautiful feature and makes a great tool for professional programmers to write codes.
But when you write functions inside of functions, it could be sometimes confusing and not good looking. So, python creators, brought the idea of “function inside of function” to the abstract feature, called “Decorator”. Decorator is defined by “@” sign before the name of function which should be decorated. Look at below codes to understand it better:
Another powerful feature which came from decorator concept, is “@property” decorator. As we know already about OOP, to take care of encapsulation, we need “getter” and “setter” methods. But sometimes, writing these lines of codes could make your code look ugly. So “@property” decorator, which is a built-in decorator of python, could set these getter and setter methods in more compact fashion. Look at below code to understand @property decorator better:
If you need to learn more about python OOP, you can take my private tutoring courses for affordable price. You can contact me from “Contact Us” tab in menu or just simply call me.