Sunday, 26 March 2023

Top 5 examples of Python decorators:

 @staticmethod: This decorator is used to define a static method in a class. A static method is a method that can be called on the class itself rather than on an instance of the class. Here's an example:

class MyClass:

    @staticmethod

    def my_static_method():

        print("This is a static method")

@classmethod: This decorator is used to define a class method in a class. A class method is a method that takes the class itself as its first argument rather than an instance of the class. Here's an example:

class MyClass:

    class_var = "Hello"

    

    @classmethod

    def my_class_method(cls):

        print(cls.class_var)

@property: This decorator is used to define a method as a property of a class. Properties allow you to access and set the value of an attribute of an instance of the class without explicitly calling a getter or setter method. Here's an example:

class MyClass:

    def __init__(self):

        self._x = 0

        

    @property

    def x(self):

        return self._x

    

    @x.setter

    def x(self, value):

        if value < 0:

            raise ValueError("Value must be non-negative")

        self._x = value

@log_calls: This decorator can be used to log all calls to a function. Here's an example:

def log_calls(func):

    def wrapper(*args, **kwargs):

        print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")

        result = func(*args, **kwargs)

        print(f"Finished {func.__name__}")

        return result

    return wrapper


@log_calls

def my_function(x, y):

    return x + y

@cache: This decorator can be used to cache the results of a function so that the function doesn't need to be called again with the same arguments. Here's an example:

def cache(func):

    results = {}

    def wrapper(*args):

        if args in results:

            return results[args]

        result = func(*args)

        results[args] = result

        return result

    return wrapper


@cache

def fibonacci(n):

    if n < 2:

        return n

    return fibonacci(n-1) + fibonacci(n-2)


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (23) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses