Search code examples
pythonpython-3.xpython-decoratorsmetaclassclass-method

Python - Metaclass decorator - How to use @classmethod


I have the following Python metaclass that adds a deco_with_args decorator to each class:

def deco_with_args(baz):
    def decorator(func):
        ...
        return func
    return decorator

class Foo(type):
    def __prepare__(name, bases):    
        return {'deco_with_args': deco_with_args}

This allows me to use the decorator like this:

class Bar(metaclass=Foo):
    @deco_with_args('baz')
    def some_function(self):
        ...

How do I make the deco_with_args decorator behave like an @classmethod so that I can access the Bar class (or whatever other class) from within the decorator function?

I have tried using @classmethod on the deco_with_args function with no luck.


Solution

  • There are two interpretations on your question - if you need cls to be available when the function named decorator in your example is called (i.e. you need your decorated methods to become class methods), it suffices that itself is transformed into a classmethod:

    def deco_with_args(baz):
        def decorator(func):
            ...
            return classmethod(func)
        return decorator
    

    The second one is if you need cls to be available when deco_with_args itself is called, when creating the decorated function itself, at class creation. The answer that is listed as accepted right now lists the straightforward problem with that: The class does not exist yet when the class body is run, so, there is no way that at the end of parsing the class body you can have methods that would have known of the class itself.

    However, unlike that answer tries to imply, that is not a real deal. All you have to do is to run your decorator code (the code that needs the cls) lazily, at the end of the class creation process. You already have a metaclass setup, so doing this is almost trivial, by just adding another callable layer around your decorator-code:

    def deco_with_args(baz):
        def outter_decorator(func):
            def decorator(cls):
                # Code that needs cls at class creation time goes here
                ...
    
                return func
            return decorator
        outter_decorator._deco_with_args = True
        return outter_decorator
    
    class Foo(type):
        def __prepare__(name, bases):    
            return {'deco_with_args': deco_with_args}
    
        def __init__(cls, cls_name, bases, namespace, **kwds):
            for name, method in cls.__dict__.items():
                if getattr(method, '_deco_with_args', False):
                    cls.__dict__[name] = method(cls)
    
            super().__init__(cls_name, bases, namespace, **kwds)
    

    This will be run, of course, after the class body execution is complete, but before any other Python statement after the class is run. If your decorator would affect other elements that are executed inside the class body itself, all you need to do is to wrap those around to warrant a lazy-execution as well.