您的位置:首页 > 移动开发 > Objective-C

Python type and object relationship

2013-06-25 12:15 267 查看


1) Everything is object in Python, even types (what the hell, how can this happen ? See below bullet points)

2) "class" and "type" are the same thing, they are "type objects". "Type objects" can be subclassed. The other objects in Python are "concrete objects" which are instantiated of "type objects" (class or type) and they can't be subclassed.

3) "type" is a concrete instance of itself (what the hell).

4) "type" is metaclass which is used to create new classes/types

5) "object" is a "class" and it is a concrete instance of "type". Note, "object" doesn't inherit from "type", instead it is instantiated of "type" by the CPython runtime. CPython CORE need do special
initialization for "type" and "object", because they are interdependent (Chicken and Egg problem). This is why "isinstance(object, type) == True".

The following pseudo code should help understand how CPython CORE init these two.

# object's class is type
object.__class__ = type
# PyType_Ready(type) sets:
type.__bases__ = (object,)
type.__class__ = type(object)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: