Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can @decorator not decorate a staticmethod or a classmethod?

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.

like image 919
user238424 Avatar asked Jan 01 '10 05:01

user238424


People also ask

How do you decorate a Classmethod?

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.

What is the difference between Classmethod and Staticmethod?

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.

What does the use of the Classmethod decorator signify?

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.

What is the purpose of Classmethod and Staticmethod in Python?

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.

How to define the decorator as a method class?

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.

How to decorate a method in a class in Java?

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.

How to use the return statement in the class method decorator?

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.

How to decorate methods in a class with Python?

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 ...


2 Answers

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' 
like image 105
wberry Avatar answered Oct 14 '22 18:10

wberry


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' 
like image 43
user238424 Avatar answered Oct 14 '22 18:10

user238424