Sitemap
Sameeramd

Sameeramd

Be happy for this movement, this movement is your life..!

Decorators in Python

3 min readNov 23, 2023

--

Decorators are Adding extra functionality to a existing function.

Decorator

  • Decorators are simply functions.
  • Like any function, decorators perform a task. The difference here is that decorators apply logic or change the behavior of other functions.
  • They are an excellent way to reuse code, and can help to separate logic into individual concerns.
  • the decorators are used to modify the behaviour of function or class. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.
  • the ‘@’ symbol followed by the decorator name above the function or class definition.
  • When you use a decorator, you’re essentially wrapping a function or class with another function, which allows you to modify its behavior. Decorators are used for tasks like logging, timing, authentication, or adding additional functionality to existing code. Here’s an example to help illustrate how decorators work:
def decorator_function(original_function):
def wrapper_function(*args, **kwargs):
# Additional code before the original function is called
print("Decorator: Before the function is called")
result = original_function(*args, **kwargs)
# Additional code after the original function is called
print("Decorator: After the function is called")
return result
return wrapper_function
@decorator_function
def greet(name):
print(f"Hello, {name}!")
greet("Sameera")

In this example, the decorator_function is defined to wrap the greet function. It adds some extra code before and after the original function is called. When we call greet("Sameera"), it will execute the wrapper function defined in the decorator, which in turn calls the original greet function. The output will be:

Decorator: Before the function is called
Hello, Sameera!
Decorator: After the function is called

This demonstrates how the decorator modifies the behavior of the original function by adding extra functionality. You can also use class-based decorators, which follow a similar concept but use classes instead of functions. They provide more flexibility and can maintain state between function calls.

methods

instance methods

    Get Sameeramd’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • these methods take self as first parameter, we use these methods to modify or alter the instance’s attributes or state.
  • These are so called because they can access unique data of their instance.
  • If you have two objects each created from a car class, then they each may have different properties. They may have different colors, engine sizes, seats, and so on.
  • Inside any instance method, you can use self to access any data or methods that may reside in your class. You won’t be able to access them without going through self.
  • Through the self parameter, instance methods can freely access attributes and other methods on the same object.
  • Through the self parameter, instance methods can freely access attributes and other methods on the same object.

class methods @classmethod

  • these methods accpets “cls” as their first attribute, we define a class method by using a decorator @classmethod.
  • Class methods know about their class. They can’t access specific instance data, but they can call other static methods.
  • class methods, on the other hand are useful for operations that need to be performed on the class as a whole, such as creating a new instance of the class, or modifying the class’s behavior in some way.
  • Instead of accepting a self parameter, class methods take a cls parameter that points to the class — and not the object instance — when the method is called.
  • Because the class method only has access to this cls argument, it can’t modify object instance state. That would require access to self.

static method @staticmethod

  • we define static methods using @staticmethods decorator, these methods accepts no cls or self as first parameters.
  • Static methods are methods that are related to a class in some way, but don’t need to access any class-specific data. you can simply call your method.
  • You may use a static method to add two numbers together, or print a given string.

Summary

  • class method allows you to define methods that are shared by all instance of a class, and can be used to define factory methods, alternative constructors or methods that modify the class itself, while instance methods are defined for each instance of the class and allows you to define the behavior of an object.

--

--

Sameeramd
Sameeramd

Be happy for this movement, this movement is your life..!