extending an existing class without inhertance

Martin Schmettow martin.schmettow at bibliothek.uni-regensburg.de
Wed Jan 8 11:17:48 CET 2003


>>Extending the class by inheritance is obvious but seems not 
>>practical in this case, because a lot is done by Factories, which
>>you cant just tell to produce MyElement instead of Element.
>>The easiest solution would be to enhance the existing class by just 
>>adding the method. I did this several times in Perl (in a 
>>former life, of course).
>>Can anybody tell me the Python way to do it. Other solutions 
>>are welcome as well.
>>
>>CU
>>Martin.
>>
sismex01 at hebmex.com wrote:
> 
> What you're asking is quite doable, and simple, in Python.
> Let me explain by example. Let's take a simple class where
> we want to add a simple (originally non-existant) method.
> 
> 
>>>>class Duck:
> 
> 	def __init__(self, name):
> 		self.name = name
> 	def __str__(self):
> 		return "<%s instance '%s' at %s>" % \
> 		       (self.__class__.__name__, self.name, hex(id(self)))
> 	__repr__ = __str__
> 
> 
>>>>d1 = Duck("Donald")
>>>>d2 = Duck("Daffy")
>>>>d1, d2
> 
> (<Duck instance 'Donald' at 0xb16e00>, <Duck instance 'Daffy' at 0xb19fb0>)
> 
> 
> So we have a class which returns 'self's info upon calling
> it's __str__ method. Simple.
> 
> Now, let's define a function which print's out --not returns--
> the information about an instance.  It's like, but not quite,
> __str__. :-)
> 
> 
>>>>def Identify(duck):
> 
> 	print "%s of name '%s', and identity %s" % \
> 	      (duck.__class__.__name__, duck.name, hex(id(duck)))
> 
> 
>>>>Identify(d1)
> 
> Duck of name 'Donald', and identity 0xb16e00
> 
>>>>Identify(d2)
> 
> Duck of name 'Daffy', and identity 0xb19fb0
> 
> 
> So it's simple-minded; this is a simple example. :-)
> Now, let's convert this ordinary function into a method,
> which is callable from any instance.
> 
> First, let's verify that this method doesn't exist:
> 
> 
>>>>d1.Identify()
> 
> Traceback (most recent call last):
>   File "<pyshell#55>", line 1, in ?
>     d1.Identify()
> AttributeError: Duck instance has no attribute 'Identify'
> 
> 
> Now, in order to "bind" this function to a class method,
> we assign it to the class object, as an attribute:
> 
> 
>>>>d1.__class__.Identify = Identify
>>>>del Identify
>>>>
> 
> 
> I deleted it from the global (module) namespace, just
> to be a bit more explicit about the original function
> not existing anymore:
> 
> 
>>>>Identify(d1)
> 
> Traceback (most recent call last):
>   File "<pyshell#60>", line 1, in ?
>     Identify(d1)
> NameError: name 'Identify' is not defined
> 
> 
> 
> So now, we try out the function, now bound as an
> instance method of the Duck class.  Although we
> didn't actually assign it to "Duck.Identify",
> remember that d1.__class__ is Duck.
> 
> 
> 
>>>>d1.Identify()
> 
> Duck of name 'Donald', and identity 0xb16e00
> 
>>>>d2.Identify()
> 
> Duck of name 'Daffy', and identity 0xb19fb0

Hi Gustavo.

Thanks for your advice. Learned some interesting python internals on it.
But in this case it didn't help. I get an AttributeError: __class__ and 
the same with __dict__ btw. I am using the Ft.Xml.Domlette (from 
4Suite). In the doc it says, that cDomlette is used internally. This is 
an  implementation in C. Might this be the problem?

CU
Martin.





More information about the Python-list mailing list