Extending objects by a method?

K

Karsten Wutzke

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
 
K

Karsten Wutzke

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
 
T

Tim Chase

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:
.... def __init__(self, greeting):
.... self.greeting = greeting
........ print "%s, %s" % (self.greeting, other)
....Hello, world


-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top