Why can decorator not decorate a staticmethod or a classmethod?
from decorator import decorator @decorator def print_function_name(function, *args): print '%s was called.' % function.func_name return function(*args) class My_class(object): @print_function_name @classmethod def get_dir(cls): return dir(cls) @print_function_name @staticmethod def get_a(): return 'a' Both get_dir and get_a result in AttributeError: <'classmethod' or 'staticmethod'>, object has no attribute '__name__'.
Why does decorator rely on the attribute __name__ instead of the attribute func_name? (Afaik all functions, including classmethods and staticmethods, have the func_name attribute.)
Edit: I'm using Python 2.6.
To decorate a method in a class, first use the '@' symbol followed by the name of the decorator function. A decorator is simply a function that takes a function as an argument and returns yet another function.
Class method can access and modify the class state. Static Method cannot access or modify the class state. The class method takes the class as parameter to know about the state of that class. Static methods do not know about class state.
The @classmethod Decorator This decorator exists so you can create class methods that are passed the actual class object within the function call, much like self is passed to any other ordinary instance method in a class.
The difference between the Class method and the static method is: A class method takes cls as the first parameter while a static method needs no specific parameters. A class method can access or modify the class state while a static method can't access or modify it.
we have used the _call_ to define the decorator as a method class in the code. The _call_ method is used when a user creates an object to work as a function, and the decorator will return the object that works like a function.
To decorate a method in a class, first use the ‘@’ symbol followed by the name of the decorator function. A decorator is simply a function that takes a function as an argument and returns yet another function.
When an operation is needed to be performed using the class decorator, we can use the return statement, which will return the value of the operation that has been executed. In this example, we performed a cube operation using the class method decorator where we have mentioned the class method with args and kwargs that are used as arguments.
Decorating Methods defined in a Class With Python 1 Decorating Methods. Decorators provide a convenient and elegant way to add functionality to functions and methods in Pyt... 2 Using the Same Decorator for Multiple Methods. To increase both the usability and the utility of decorators, we can... 3 Summary. More ...
classmethod and staticmethod return descriptor objects, not functions. Most decorators are not designed to accept descriptors.
Normally, then, you must apply classmethod and staticmethod last when using multiple decorators. And since decorators are applied in "bottom up" order, classmethod and staticmethod normally should be top-most in your source.
Like this:
class My_class(object): @classmethod @print_function_name def get_dir(cls): return dir(cls) @staticmethod @print_function_name def get_a(): return 'a'
It works when @classmethod and @staticmethod are the top-most decorators:
from decorator import decorator @decorator def print_function_name(function, *args): print '%s was called.' % function.func_name return function(*args) class My_class(object): @classmethod @print_function_name def get_dir(cls): return dir(cls) @staticmethod @print_function_name def get_a(): return 'a'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With