Quantcast
Channel: Implementing the decorator pattern in Python - Stack Overflow
Browsing all 8 articles
Browse latest View live

Answer by David Taub for Implementing the decorator pattern in Python

In Python 3, Philipp's accepted answer raised RuntimeError: maximum recursion depth exceeded.The way that worked for me:class Foo(object): def f1(self): print("original f1") def f2(self):...

View Article



Answer by Milan Falešník for Implementing the decorator pattern in Python

In one of my projects, I also needed to do one particular thing, that is that even the underlying object should actually execute the method that was reimplemented in the decorator. It is actually quite...

View Article

Answer by synack for Implementing the decorator pattern in Python

To complement @Alec Thomas reply. I modified his answer to follow the decorator pattern. This way you don't need to know the class you're decorating in advance.class Decorator(object): def __new__(cls,...

View Article

Answer by Alec Thomas for Implementing the decorator pattern in Python

As an addendum to Philipp's answer; if you need to not only decorate, but preserve the type of an object, Python allows you to subclass an instance at runtime:class foo(object): def f1(self): print...

View Article

Answer by IanH for Implementing the decorator pattern in Python

The UML diagram in the linked Wikipedia article is wrong and so is your code.If you follow the "decorator pattern", the decorator class is derived from the base decorated class. (In the UML diagram an...

View Article


Answer by Philipp for Implementing the decorator pattern in Python

You could use __getattr__:class foo(object): def f1(self): print "original f1" def f2(self): print "original f2"class foo_decorator(object): def __init__(self, decoratee): self._decoratee = decoratee...

View Article

Answer by andyortlieb for Implementing the decorator pattern in Python

It's arguably not the best practice, but you can add functionality to instances, as I've done to help transition my code from Django's ORM to SQLAlachemy, as follows:def _save(self): session.add(self)...

View Article

Implementing the decorator pattern in Python

I want to implement the decorator pattern in Python, and I wondered if there is a way to write a decorator that just implements the function it wants to modify, without writing boiler-plate for all the...

View Article

Browsing all 8 articles
Browse latest View live


Latest Images