Should proxy objects lie about their class name?

Steven Bethard steven.bethard at gmail.com
Tue Nov 27 11:55:46 EST 2007


Fuzzyman wrote:
> On Nov 26, 11:56 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
>> On Nov 20, 3:50 pm, j... at pobox.com (John J. Lee) wrote:
>>> Not much to add to the subject line.  I mean something like this:
>>> ProxyClass.__name__ = ProxiedClass.__name__
>>> I've been told that this is common practice.  Is it?  Would this
>>> surprise you if you ran into it in a debugging session?
>>> One very real advantage that I can see is avoiding breaking existing
>>> doctests.
>>
>> Python 3.0 has a proposal, accepted I believe, to allow classes to
>> control the behavior of issubclass and ininstance, so there appears to
>> be tacit support from the language for mimicking the proxied classes
>> in such ways.
> 
> In Python 2 you can already lie to 'isinstance' and 'issubclass' by
> catching calls to the '__class__' and '__bases__' attribute. I'm not
> sure yet whether this is a good thing however. :-)

The Python 3 machinery allows *other* classes to lie about whether or 
not your object is an instance or subclass of them, without requiring 
them to set your __class__ or __bases__.  So, for example, you can 
create a class ``Integer`` and make ``issubclass(int, Integer)`` true. 
For more information see:

     http://www.python.org/dev/peps/pep-3119/

STeVe



More information about the Python-list mailing list