From: Karsten Wutzke on 15 Jul 2010 07:35 Hello, I'm new to Python so beware. I have a hierarchical object structure which I iterate over (the elements/classes of interest). I would like to apply the Visitor pattern onto this object structure, so I must add an "accept" method to every object (I'm interesting in) for the Visitor pattern to work. Is there any Python-inbuilt way to dynamically add a method or do I have to wrap each iterated object into a Decorator supplying the accept method? The latter would mean having to rebuild the (part) hierarchy with the Decorator objects before running the Visitor, which I'd like to avoid. Karsten
From: Karsten Wutzke on 15 Jul 2010 07:45 Small correction: I probably have to add a method to a class, so that every object instantiated not by me has the desired functionality. Karsten
From: Tim Chase on 15 Jul 2010 09:58 On 07/15/2010 06:45 AM, Karsten Wutzke wrote: > Small correction: I probably have to add a method to a class, so that > every object instantiated not by me has the desired functionality. You mean like: >>> class Foo: .... def __init__(self, greeting): .... self.greeting = greeting .... >>> f = Foo("Hello") >>> def greet(self, other): .... print "%s, %s" % (self.greeting, other) .... >>> Foo.greet = greet >>> f.greet("world") Hello, world -tkc
|
Pages: 1 Prev: python3: help with pickle Next: empty set and empty dict for Python 3 |