Home > Backend Development > Python Tutorial > How to Override Decorator Arguments in Python

How to Override Decorator Arguments in Python

Susan Sarandon
Release: 2025-01-10 08:06:41
Original
704 people have browsed it

How to Override Decorator Arguments in Python

To modify decorator arguments within a child class method inherited from a parent class, you must explicitly override the method itself. Simply defining new class variables with matching names won't alter the decorator's behavior. The decorator arguments are bound at the time the method is decorated, not when the class is instantiated.

Illustrative Example

The following Python code (test.py) demonstrates this:

<code class="language-python">def my_decorator_with_args(param1, param2):
    """Decorator accepting arguments"""
    def actual_decorator(func):
        def wrapper(self, *args, **kwargs):
            print(f"[Decorator] param1={param1}, param2={param2}")
            return func(self, *args, **kwargs)
        return wrapper
    return actual_decorator


class BaseClass:
    @my_decorator_with_args(param1="BASE_PARAM1", param2="BASE_PARAM2")
    def greet(self):
        print("Hello from BaseClass!")


class DerivedClass(BaseClass):
    """
    Attempting to override decorator arguments via class variables;
    however, since `greet()` isn't redefined, the parent's decorator remains active.
    """
    param1 = "DERIVED_PARAM1"
    param2 = "DERIVED_PARAM2"


class DerivedClassOverride(BaseClass):
    """
    Correctly overrides `greet()` to utilize modified decorator arguments.
    """
    @my_decorator_with_args(param1="OVERRIDE_PARAM1", param2="OVERRIDE_PARAM2")
    def greet(self):
        print("Hello from DerivedClassOverride!")


if __name__ == "__main__":
    print("=== BaseClass's greet ===")
    b = BaseClass()
    b.greet()

    print("\n=== DerivedClass's greet (no override) ===")
    d = DerivedClass()
    d.greet()

    print("\n=== DerivedClassOverride's greet (with override) ===")
    d_o = DerivedClassOverride()
    d_o.greet()
</code>
Copy after login

Executing python test.py produces:

<code>=== BaseClass's greet ===
[Decorator] param1=BASE_PARAM1, param2=BASE_PARAM2
Hello from BaseClass!

=== DerivedClass's greet (no override) ===
[Decorator] param1=BASE_PARAM1, param2=BASE_PARAM2
Hello from BaseClass!

=== DerivedClassOverride's greet (with override) ===
[Decorator] param1=OVERRIDE_PARAM1, param2=OVERRIDE_PARAM2
Hello from DerivedClassOverride!</code>
Copy after login

This clearly shows that only by redefining the method (greet) in the child class can you successfully override the decorator's arguments.

The above is the detailed content of How to Override Decorator Arguments in Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template