Type of an object:

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Dec 17 23:15:03 CET 2013


Steven D'Aprano wrote:
> Well, that is a surprise, but I don't think that is intended behaviour. I 
> think that's something which only works by accident. The intention is 
> that __class__ returns the instance's type, not arbitrary values.

Well, a proxy object would obviously return a suitable
class-like object. I was just demonstrating that it's
possible to override what __class__ returns.

I don't think it's an accident, because the weakref
module uses this for its proxy objects.

 >>> import weakref
 >>> class C(object):
...  pass
...
 >>> c = C()
 >>> p = weakref.proxy(c)
 >>> p.__class__
<class '__main__.C'>
 >>> type(p)
<type 'weakproxy'>

> If you 
> try to set it to a non-class on the instance, it fails:

For proxying purposes you don't need to be able to set
it, but I don't see why you couldn't use a property
setter to override that behaviour as well if you really
wanted to.

-- 
Greg



More information about the Python-list mailing list