python - python中的类类型

标签 python class types new-style-class

为什么我这样做:

class C(): pass
type(C())

我得到:<type 'instance'> ,但如果我这样做:

class C(object): pass
type(c())

我得到:<class '__main__.c'>

第一个不是很有用

最佳答案

查看旧式类和新式类之间的区别。前者是默认的,后者显式地从对象继承。

所有旧式对象都是使用内置类型实例实现的。事实上,它们仍然是默认值并且它们的类型仍然是“实例”,这是向后兼容性预防措施的结果。

这是从 Python 文档 (http://docs.python.org/reference/datamodel.html) 中提取的

3.3. New-style and classic classes Classes and instances come in two flavors: old-style (or classic) and new-style.

Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.class designates the class of x, but type(x) is always . This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.

New-style classes were introduced in Python 2.2 to unify classes and types. A new-style class is neither more nor less than a user-defined type. If x is an instance of a new-style class, then type(x) is typically the same as x> .class (although this is not guaranteed - a new-style class instance is permitted to override the value returned for x.class).

The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model. It also has a number of practical benefits, like the ability to subclass most built-in types, or the introduction of “descriptors”, which enable computed properties.

For compatibility reasons, classes are still old-style by default. New-style classes are created by specifying another new-style class (i.e. a type) as a parent class, or the “top-level type” object if no other parent is needed. The behaviour of new-style classes differs from that of old-style classes in a number of important details in addition to what type() returns. Some of these changes are fundamental to the new object model, like the way special methods are invoked. Others are “fixes” that could not be implemented before for compatibility concerns, like the method resolution order in case of multiple inheritance.

While this manual aims to provide comprehensive coverage of Python’s class mechanics, it may still be lacking in some areas when it comes to its coverage of new-style classes. Please see http://www.python.org/doc/newstyle/ for sources of additional information.

Old-style classes are removed in Python 3.0, leaving only the semantics of new-style classes.of new-style classes.

关于python - python中的类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886117/

相关文章:

python - 从字符串列表中删除前缀引号

python - YouTube Analytics API - 内容所有者按 channel 分组的总观看次数

python - 未找到资源语料库/wordnet(运行 quepy dbpedia 示例应用程序)

java - 在 Java 中强制转换为 String 或使用 String.valueOf()

python - 在可以返回不同类型参数的函数中使用类型提示是否有意义?

typescript - 如何使用 TypeScript 从接口(interface)动态推断类型?

Python Decimal 模块不适用于 uint64

python - 在 Python 类中调用 odeint

python - 更新多个属性python

java - 使用 ArrayList 类型错误?