fiddling a class's __bases__ attribute

Skip Montanaro skip at mojam.com
Wed Sep 6 00:06:46 EDT 2000


I decided that for my CGI scripts I wanted FormContentDict objects to allow
items to be deleted, but I didn't want to create my own version of cgi.py
just to add a __delitem__ method.  This was accomplished easily enough:

    class FormContentDictBase:
	def __delitem__(self,key):
	    del self.dict[key]

    import cgi
    FormContentDictBase.__bases__ = cgi.FormContentDict.__bases__
    cgi.FormContentDict.__bases__ = (FormContentDictBase,)

I'm pretty certain that __bases__ has been writable for awhile (since 1.5?).
Aside from the slight underhandedness of the technique is there anything
else I should be careful of?

By the way, has anyone else noticed that the Deja News search box on
http://www.python.org/search/ seems busted?  I searched there for __bases__
and got some hits pretty far afield from comp.lang.python
(alt.religion.kibology and sci.physics.relativity, to name two other
groups).

-- 
Skip Montanaro (skip at mojam.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list