Decorator metaclass

Thomas Karolski Thomas.Karolski at googlemail.com
Fri May 23 11:42:50 EDT 2008


Thanks for pointing out all those mistakes. I think I'm already starting 
to grasp all of the python magic going on in there.

> Parenthetical: I don't normally recommend this style, since it
> obscures the fact that you're using a custom metaclass to the user.
> That is something the user probably would benefit from knowing, if for
> no other reason than so they can make a mental note about where to
> look first if something goes wrong.  I prefer to make the user use the
> __metaclass__ attribute.

Really just personal preference I think. I'm not really a friend of 
declaring variables if there is a more "intuitive" way.

>> class HBar(Decorator):
>>         def __init__(self, number):
>>                 Decorator.__init__(self)
> 
> Ok, at this point you have to ask yourself what you want to do,
> because the solution you choose will involve trade-offs.

Yes, it was probably a bad example. I decided not to call the Decorator 
's __init__ method in my new version (which I have posted as a reply to 
the reply of Maric Michaud).

> You will note that Decorator does not define __init__.  In fact,
> object.__init__ will be called, which does nothing.  If you think that
> all classes with DecoratorType as their metaclass will be a direct
> subclass of Decorator, you can get away with not calling
> Decorator.__init__ at all.

Now, inside my new version, I have a class which inherits from both 
Decorator and Window, out of which the __init__ for Decorator is not 
called. Does this prove to be a problem?

> Probably the best piece of advice is "Don't try to use Decorator
> pattern".  :)

Well, I decided on the decorator pattern, because I want to be able to 
change the behavior of classes during run-time. I did not really find 
any other pattern which would let me do that.

Regards,
Thomas K.




More information about the Python-list mailing list