Python 中的类型和类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4479819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:55:23  来源:igfitidea点击:

Types and classes in Python

pythonclasstypes

提问by sasuke

I'm a bit confused about types and classes in Python. For e.g. the following REPL conversation confuses me:

我对 Python 中的类型和类有点困惑。例如,以下 REPL 对话让我感到困惑:

>>> class A: pass
... 
>>> a = A()
>>> type(a)
<type 'instance'>
>>> a.__class__
<class __main__.A at 0xb770756c>
>>> type([])
<type 'list'>
>>> [].__class__
<type 'list'>
>>> type(list)
<type 'type'>
>>> list.__class__
<type 'type'>
>>> type(A)
<type 'classobj'>
>>> A.__class__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__class__'
  1. Why is the type and class for inbuilt things (e.g. list here) the same but different for user classes/types?
  2. Isn't every class an instance of some other class (like Class in Java)? Why no __class__for user defined classes?
  1. 为什么内置事物的类型和类(例如此处列出)与用户类/类型相同但不同?
  2. 不是每个类都是其他类的实例(如 Java 中的类)吗?为什么不是__class__用户定义的类?

Any explanation/further reading which can clarify this behaviour would be much appreciated. TIA.

任何可以澄清这种行为的解释/进一步阅读将不胜感激。TIA。

采纳答案by wheaties

You're encountering the different behavior for new style classes versus classic classes. For further reading read this: Python Data Model. Specifically read the section on classes and the difference between new style and classic classes.

您会遇到新样式类与经典类的不同行为。如需进一步阅读,请阅读:Python 数据模型。具体阅读类和新式与经典类的区别部分。

Try typing the following into your REPL:

尝试在您的 REPL 中输入以下内容:

class A: pass
class B(object): pass

and you'll see that you get different results. Here you're dealing with the difference between new style and old style classes. Using Python 2.6.1 here's what I get:

你会看到你得到不同的结果。在这里,您正在处理新样式类和旧样式类之间的区别。使用 Python 2.6.1 这是我得到的:

> type(A)
<type "classobj">
> type(B)
<type "type">

which tells you that lists are new style classes and not old style classes. We can further play around with things using listas well:

它告诉您列表是新样式类而不是旧样式类。我们还可以进一步使用以下内容list

> type(list)
<type "type">

same as our class B(object): passresult. And also

和我们的class B(object): pass结果一样。并且

> c = []
> type(c)
<type "list">

which is telling you about the instance of the object and not it's definition.

这是告诉你对象的实例而不是它的定义。

回答by Lennart Regebro

It's "Hystorical reasons". Or possible "Histerical". It's all fixed in Python 3:

这是“历史原因”。或者可能的“历史”。这一切都在 Python 3 中修复了:

>>> class A: pass
... 
>>> a = A()
>>> type(a)
<class '__main__.A'>
>>> a.__class__
<class '__main__.A'>
>>> type([])
<class 'list'>
>>> [].__class__
<class 'list'>
>>> type(list)
<class 'type'>
>>> list.__class__
<class 'type'>
>>> type(A)
<class 'type'>
>>> A.__class__
<class 'type'>
>>> class B(object): pass
... 
>>> type(B)
<class 'type'>
>>> b = B()
>>> type(b)
<class '__main__.B'>

回答by user2352151

In Python 3.0, user-defined class objects are instances of the object named type, which is itself a class. ? In Python 2.6, new-style classes inherit from object, which is a subclass of type; ? classic classes are instances of type and are not created from a class.

在 Python 3.0 中,用户定义的类对象是名为 type 的对象的实例,它本身就是一个类。? 在 Python 2.6 中,新式类继承自 object,它是 type 的子类;? 经典类是类型的实例,不是从类创建的。