Subclassing a read-only class

Alex Martelli aleax at aleax.it
Mon Feb 18 11:21:42 EST 2002


Gerhard Häring wrote:

> Hello,
> 
> For compatibility testing, I'd like to put an additional method into
> window object I get from the curses module. Unfortunately, if I try to
> do so, I get:
> 
>     TypeError: object has read-only attributes
> 
> I'm pretty sure there is a solution, probably with a proxy object. But
> currently I just don't get it.

You can generally substitute automatic-delegation in lieu of
inheritance (as long as nobody is so uncouth as to typetest,
e.g. with isinstance -- in which case, you're in trouble).

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295 gives
a simple example ('inheriting' [via automatic delegation] from a
file object back in Python 2.0 / 2.1 when you couldn't actually
just subclass file as you can today) -- I hope you find the idea
easy to adapt to your specific needs.


Alex





More information about the Python-list mailing list