top of page

Python code optimization, and Built-in Decorators

Updated: Jan 28, 2023

@lru_cache speed up your program by caching,

This decorator can be used to cache the results of a function, so that subsequent

calls to the function with the same arguments will not be executed again.

this method called every time, for same parameters
this method called every time, for same parameters

this method called only once for the same params

@lru_cache decorator has parameter maxsize, that specifies the maximum number of results to store in the cache. When the cache is full, new result will be

stored, by removing the least recently used result. It is called Least Recently Used strategy.


@total_ordering: A class decorator that fills, missing ordering methods.

@total_ordering decorator generate the missing comparison methods for a python class, based on the ones that are defined.

Above code has not defined __ge__, or __le__ methods in Employee class,

However @total_ordering decorator will add those methods.


@contextmanager make a customized context manager, it manage resources properly.

above code open a file using the 'with' statement, so it will be closed automatically, after being written.

sometimes we need to perform some operations after opening a file, and before closing a file.

demo.txt file is opening...
demo.txt is closing... 

@property setter, and getter properties of python class setter, and getter are encapsulation concept of OOPS.

getter, and setters are accessor and mutators, setters ensures valid information

in the object, and protects the object data.

ValueError: age must be in between 1 - 150

@cached_property is a decorator which transforms a method of a class into a property whose value is computed only once and then cached as a normal attribute.

Why caching?
The cache memory is a high-speed memory available inside CPU in order to speed up access to data and instructions. Therefore, the cache is a place that is quick to access. The result can be computed and stored once and from next time, the result can be accessed without re-computing it again. So, it is useful in case of expensive computations.

once cached property accessed i.e. computed, and later your object state changes, it will not perform the calculation again.

calculating total hours...

@classmethod decorator will define a class method in class

@staticmethod decorator will define a static method in class

Python class can have 3 types of methods.

instance methods methods that are bound to an instance. Instance method called on an instance of the class, they can modify the state of the object. instance methods first parameter is self, with self parameter it can access the data of the object.

class methods methods that are bound to the class. A class method is called with the help of class_name only, they can't modify the instance data. class methods first parameter is cls by conventionally.

static methods methods that are not bound to the instance or the class. They are included in class, because they logically belongs to the class. usually static methods

are defined as utility classes to perform a group of related tasks.

Employees count:  0
Nagesh, 78000.0     
Satish, 67000.0     
Employees count:  2 
employee deleted
Employees count:  1
Hike Details...
1-5 years, 30% HIKE
6-10 years, 25% HIKE
11-15 years, 10% HIKE
anyone, 5% HIKE
leaving __main__ scope. rest of the objects garbage collected.
employee deleted

@dataclass decorator can automatically generate several methods for a class such as __init__, __repr__, __eq__, __lt__ and so on.

rectangle drawn from Point(x=10, y=20) to Point(x=80, y=40)

@atexit.register register a function to be executed upon normal program termination.

@register decorator defined in atexit module.

This decorator performing final tasks, such as releasing resources, and logging the


application started.
exiting __main__ scope. 
Thanks for reading!!!

Your Rating and Review will be appreciated!!

Twitter: @LearnerLandmark

Recent Posts

See All
bottom of page