Python Type System - Object vs Type

后端 未结 2 1222
花落未央
花落未央 2021-01-02 12:19

I am new to Python. I am familiar with Java, C/C++, and OCaml. I understand Lambda Calculus and elementary Type Theory because of a Programming Languages course I took at Un

相关标签:
2条回答
  • 2021-01-02 12:56

    Because the notion of type is the same as class you can subclass type with normal object-oriented techniques and class syntax to customize it.

    And because classes are really instances of the type class, creating classes from customized subclasses of type allows to implement custom kinds of classes.

    In new-style classes:

    • type is a class that generates user-defined classes.
    • Metaclasses are subclasses of the type class.
    • Class objects are instances of the type class, or a subclass thereof.
    • Instance objects are generated from a class.
    0 讨论(0)
  • It is talking specifically about the object type. For type, all types inherit from <type 'type'>.

    I believe the other statement is just pointing out that <type 'type'> is an object; an example I thought of from looking further down:

    t = list.__class__ # <type 'type'>
    t.__bases__ # (<type 'object'>,)
    
    0 讨论(0)
提交回复
热议问题