Extending objects by a method?

Tim Chase python.list at tim.thechases.com
Thu Jul 15 09:58:53 EDT 2010


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






More information about the Python-list mailing list