Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27523
| References | <21067364-7b9c-4415-bad1-947961497343@u20g2000yqc.googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-08-20 12:39 -0600 |
| Subject | Re: Class.__class__ magic trick help |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3576.1345488010.4697.python-list@python.org> (permalink) |
On Mon, Aug 20, 2012 at 12:01 PM, Massimo Di Pierro <massimo.dipierro@gmail.com> wrote: > I discovered I can do this: > > class A(object): pass > class B(object): > __class__ = A # <<<< magic > > b = B() > isinstance(b,A) # returns True (as if B derived from A) > isinstance(b,B) # also returns True > > I have some reasons I may want to do this (I an object with same > methods as a dict but it is not derived from dict and I want > isinstance(x,dict)==True to use it in place of dict in some other > code). > > What are the problems with the magic trick above? Why does it work? Normally with __class__ assignment, you would assign to the __class__ attribute of the *instance*, not the class declaration. This actually changes the class of the object, and so isinstance(b, B) would no longer return True. I've never heard of assigning it in the class declaration, and as far as I know, this behavior isn't documented anywhere. I expect that what's happening here is that Python is not actually updating the class of the instance, but that A is merely assigned to the "__class__" attribute in the class dict, and that isinstance is somehow (perhaps accidentally) finding this. So I think this is probably a bug, and I would not rely on it to work correctly in all cases. In any event, the use case that you're looking for is usually accomplished using abstract base classes. Instead of "isinstance(x, dict)", you should use "isinstance(x, collections.MutableMapping)", and then inherit your class from or register it with the MutableMapping ABC.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Class.__class__ magic trick help Massimo Di Pierro <massimo.dipierro@gmail.com> - 2012-08-20 11:01 -0700
Re: Class.__class__ magic trick help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-20 18:21 +0000
Re: Class.__class__ magic trick help Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:39 -0600
Re: Class.__class__ magic trick help Massimo Di Pierro <massimo.dipierro@gmail.com> - 2012-08-20 12:28 -0700
Re: Class.__class__ magic trick help Massimo Di Pierro <massimo.dipierro@gmail.com> - 2012-08-20 21:17 -0700
Re: Class.__class__ magic trick help Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-08-21 08:40 +0100
Re: Class.__class__ magic trick help Massimo Di Pierro <massimo.dipierro@gmail.com> - 2012-08-21 05:52 -0700
Re: Class.__class__ magic trick help Massimo Di Pierro <massimo.dipierro@gmail.com> - 2012-08-21 06:50 -0700
csiph-web