Quantcast
Viewing all articles
Browse latest Browse all 14126

class decorator with parameter in python, raises TypeError: missing 1 required positional argument: 'type'

The following code:

    def myDecorator(cls, type):        class wrapper(cls):            contaVarClasse = 0            def __init__(cls, *args, **kwargs):                for value in cls.__dict__.values():                    if isinstance(value, type):                        cls.contaVarClasse += 1                print(cls.__dict__)        return wrapper    @myDecorator(str)    class MyClass:        countme = "b"        notme = 2        def __init__(self):            dontcountme = "a"            norme = 5    print(MyClass.contaVarClasse)

raises the "TypeError: missing 1 required positional argument: 'type'" error

From my knowledge @myDecorator(str) should be equal MyClass = myDecorator(MyClass, str), but I'm clearly wrong and I've been pretty much unable to find what I did wrong so far.

After 40 minute of googling "decorators with parameters" and "class decorator with parameters" and "TypeError: missing 1 required positional decorator" I just found all of the questions and answers to be related to decorators being applied to classes' methods, and nobody actually trying to decorate a class with another class.

I apologize if this question irritates you. Please understand I'm not trying to lazily throw this question out.

So for anybody wondering, the class decorator "myDecorator" should decorate MyClass with a variable (contaVarClasse) that should count how many class variables of type type in the decoratored class there are.


Viewing all articles
Browse latest Browse all 14126

Trending Articles