Bdfy
Bdfy

Reputation: 24621

Error using simple decorator in Python

def simple_decorator(f):
    def tmp(*args, **kwargs):
        res = f(*args, **kwargs)
        return res
    return tmp

@simple_decorator
class FirstClass():
    pass


@simple_decorator
class SecondClass(FirstClass):
    pass

I have error:

File "1.py", line 16, in <module>
class SecondClass(FirstClass):
TypeError: Error when calling the metaclass bases
function() argument 1 must be code, not str

Why ?

Upvotes: 0

Views: 173

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798546

Your decorator returns a function, so FirstClass is a function, not a class; decorators are not required to return an object that is the same type as the input.

Upvotes: 6

Zaur Nasibov
Zaur Nasibov

Reputation: 22659

Your mistake is that you're trying to decorate a class. Check PEP-3129.

Upvotes: 0

Related Questions