__class__ and type() implementation details in CPython

Discussion in 'Python' started by Ivan Yurchenko, Feb 10, 2013.

  1. Hello.

    I've done the following in CPython 2.7.3 and 3.3.0 (and also in PyPy 2.0b1):

    >>> import weakref
    >>> x = set()
    >>> y = weakref.proxy(x)
    >>> x.__class__, type(x), isinstance(x, set)

    (<class 'set'>, <class 'set'>, True)
    >>> y.__class__, type(y), isinstance(y, set)

    (<class 'set'>, <class 'weakproxy'>, True)

    So, type doesn't use object's __class__ to determine its class. I'm looking
    for some CPyhton implementation details - how does class identification with
    type() work? According to CPython's sources it looks like there is a "marker"
    of actual object's class associated with each PyObject - _typeobject struct,
    which is used to identify the class by type(). Am I right?

    Thank you.

    - Ivan Yurchenko.
    Ivan Yurchenko, Feb 10, 2013
    #1
    1. Advertising

  2. Ivan Yurchenko wrote:

    > Hello.
    >
    > I've done the following in CPython 2.7.3 and 3.3.0 (and also in PyPy
    > 2.0b1):
    >
    >>>> import weakref
    >>>> x = set()
    >>>> y = weakref.proxy(x)
    >>>> x.__class__, type(x), isinstance(x, set)

    > (<class 'set'>, <class 'set'>, True)
    >>>> y.__class__, type(y), isinstance(y, set)

    > (<class 'set'>, <class 'weakproxy'>, True)
    >
    > So, type doesn't use object's __class__ to determine its class.


    Sometimes it does:

    py> class X(object): pass
    ....
    py> class Y(object): pass
    ....
    py> x = X()
    py> x
    <__main__.X object at 0xb7c9adac>
    py> x.__class__ = Y
    py> x
    <__main__.Y object at 0xb7c9adac>
    py> type(x)
    <class '__main__.Y'>


    I believe that it depends on whether the instance being inspected is a
    heap-type (e.g. pure-Python object) or not.


    > I'm
    > looking for some CPyhton implementation details - how does class
    > identification with type() work? According to CPython's sources it looks
    > like there is a "marker" of actual object's class associated with each
    > PyObject - _typeobject struct, which is used to identify the class by
    > type(). Am I right?



    I'm not an expert on the CPython implementation, but I believe the code you
    want is buried somewhere in here:


    http://hg.python.org/cpython/file/tip/Objects/typeobject.c


    --
    Steven
    Steven D'Aprano, Feb 10, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gregory lielens
    Replies:
    6
    Views:
    600
    Gregory Lielens
    Dec 2, 2004
  2. Barry Kelly
    Replies:
    5
    Views:
    582
    Barry Kelly
    Jun 21, 2006
  3. Replies:
    1
    Views:
    407
  4. Terry Reedy
    Replies:
    6
    Views:
    318
    Steven D'Aprano
    Mar 18, 2010
  5. Steven D'Aprano
    Replies:
    10
    Views:
    133
    Gregory Ewing
    Dec 18, 2013
Loading...

Share This Page