Class.__class__ magic trick help

Massimo Di Pierro massimo.dipierro at gmail.com
Mon Aug 20 14:01:36 EDT 2012


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?

massimo





More information about the Python-list mailing list