开发者

Inheriting from decorated classes

开发者 https://www.devze.com 2023-04-05 19:17 出处:网络
I\'m trying to decorate a class with another class. I also want to inherit from the decorated class, but I get some errors. Here\'s my code:

I'm trying to decorate a class with another class. I also want to inherit from the decorated class, but I get some errors. Here's my code:

class Decorator:
    def __init__(self, decorated):
        pass

@Decorator
class Foo:
    开发者_高级运维pass

class Goo(Foo):
    pass

The error I get when I try to subclass from Foo is this:

Traceback (most recent call last):

   File "test.py", line 9, in

      class Goo(Foo):

TypeError: __init__() takes exactly 2 positional arguments (4 given)

By adding another init function to Decorator...

def __init__(self, *args):
    for arg in args:
        print(arg)

... I get the following output:

<class '__main__.Foo'>

Goo

(<__main__.Decorator object at 0x010073B0>,)

{'__module__': '__main__'}

What are those parameters and how should I be using them inside Decorator?


I'll try to answer the "what are those parameters" question. This code:

@Decorator
class Foo:
    pass

is equivalent to:

class Foo:
    pass
Foo = Decorator(Foo)

This means that Foo ends up being an instance of the Decorator class instead of being a class.

When you try to use this instance as a base of a class (Goo), Python will have to determine a metaclass that will be used to create the new class. In this case it will use Foo.__class__ which equals to Decorator. Then it will call the metaclass with (name, bases, dict) arguments and expect it to return a new class.

This is how you end up with these arguments in Decorator.__init__.

More about this can be found here: http://www.python.org/download/releases/2.2.3/descrintro/#metaclasses (particularly the "When a class statement is executed..." part)


Are you trying to add a MixIn to a class after the class has been defined? If so, you could inject the MixIn this way:

def inject_class(mixin):
    def _inject_class(cls):
        return type(cls.__name__,(mixin,)+cls.__bases__,dict(cls.__dict__))
    return _inject_class

class MixIn(object):
    def mix(self):
        print('mix')

@inject_class(MixIn)
class Foo(object):
    def foo(self):
        print('foo')

class Goo(Foo):
    def goo(self):
        print('goo')

goo=Goo()
goo.mix()
goo.foo()
goo.goo()

prints

mix
foo
goo

If you don't want the generality of inject_class, you could make a specialized class decorator which mixes in Decorator only:

def decorate(cls):
    class Decorator(object):
        def deco(self):
            print('deco')
    return type(cls.__name__,(Decorator,)+cls.__bases__,dict(cls.__dict__))

@decorate
class Foo(object):
    def foo(self):
    print('foo')

the result is the same.


I had the same problem and the following solution works for me:

from functools import update_wrapper
class decoratorBase():
    def __new__(cls, logic):
        self = object.__new__(cls)
        self.__init__(logic)
        def new (cls):
            #cls is the decorated class type, not the decorator class type itself
            self._createInstance(cls)
            self._postInstanceCreation()
            return self
        self._logic.__new__ = new
        #return the wrapped class and not a wrapper
        return self._logic
    def __init__(self, logic):
        #logic is the decorated class
        self._logic = logic
    def _createInstance(self, cls):
        self._logicInstance = object.__new__(cls)
        self._logicInstance.__init__()
    def _postInstanceCreation(self):
        pass

class factory(decoratorBase):
    def __init__(self, *largs, **kwargs):
        super().__init__(*largs, **kwargs)
        self.__instance = None
    def _createInstance(self, cls):
        self._logicInstance = None
        self._cls = cls
    def _postInstanceCreation(self):
        update_wrapper(self, self._cls)
    def __call__(self, userData, *largs, **kwargs):
        logicInstance = object.__new__(self._cls)
        logicInstance.__init__(*largs, **kwargs)
        logicInstance._update(userData)
        return logicInstance

class singelton(decoratorBase):
    def _postInstanceCreation(self):
        update_wrapper(self, self._logicInstance)
    def __call__(self, userData):
        self._logicInstance._update(userData)
        return self._logicInstance

class base():
    def __init__(self):
        self.var = 0
        print ("Create new object")
    def __call__(self):
        self.var += self._updateValue()
    def _update(self, userData):
        print ("Update object static value with {0}".format(userData))
        self.var = userData

@factory
class factoryTestBase(base):

    def __call__(self):
        super().__call__()
        print("I'm a factory, here is the proof: {0}".format(self.var))
    def _updateValue(self):
        return 1

class factoryTestDerived(factoryTestBase):
    def _updateValue(self):
        return 5

@singelton
class singeltonTestBase(base):
    def __call__(self):
        super().__call__()
        print("I'm a singelton, here is the proof: {0}".format(self.var))
    def _updateValue(self):
        return 1

class singeltonTestDerived(singeltonTestBase):
    def _updateValue(self):
        return 5

The magic in this approach is the overloading of the __new__() method, as well for the decorator itself as for the "wrapper" which is returned by the decorator. I set the word wrapper in quotes, because actually there is no wrapper. Instead the decorated class is alternated by the decorator and returned. Using this scheme, you are able to inherit from a decorated class. The most important thing is the change of the __new__() method of the decorated class, which is made by the following lines:

        def new (cls):
            self._createInstance(cls)
            self._postInstanceCreation()
            return self
        self._logic.__new__ = new

Using this, you have access to the decorator methods like self._createInstance() during creation of an object from a decorated class. You even have the opportunity to inherit from your decorators (as it is shown in the example).

Now lets run a simple example:

>>> factoryObjCreater = factoryTestBase()
>>> factoryObj1 = factoryObjCreater(userData = 1)
Create new object
Update object static value with 1
>>> factoryObj2 = factoryObjCreater(userData = 1)
Create new object
Update object static value with 1
>>> factoryObj1()
I'm a factory, here is the proof: 2
>>> factoryObj2()
I'm a factory, here is the proof: 2
>>> factoryObjDerivedCreater = factoryTestDerived()
>>> factoryObjDerived1 = factoryObjDerivedCreater(userData = 2)
Create new object
Update object static value with 2
>>> factoryObjDerived2 = factoryObjDerivedCreater(userData = 2)
Create new object
Update object static value with 2
>>> factoryObjDerived1()
I'm a factory, here is the proof: 7
>>> factoryObjDerived2()
I'm a factory, here is the proof: 7
>>> singeltonObjCreater = singeltonTestBase()
Create new object
>>> singeltonObj1 = singeltonObjCreater(userData = 1)
Update object static value with 1
>>> singeltonObj2 = singeltonObjCreater(userData = 1)
Update object static value with 1
>>> singeltonObj1()
I'm a singelton, here is the proof: 2
>>> singeltonObj2()
I'm a singelton, here is the proof: 3
>>> singeltonObjDerivedCreater = singeltonTestDerived()
Create new object
>>> singeltonObjDerived1 = singeltonObjDerivedCreater(userData = 2)
Update object static value with 2
>>> singeltonObjDerived2 = singeltonObjDerivedCreater(userData = 2)
Update object static value with 2
>>> singeltonObjDerived1()
I'm a singelton, here is the proof: 7
>>> singeltonObjDerived2()
I'm a singelton, here is the proof: 12
>>>  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号