Python 3 中对象和类之间的关系

发布于 2024-10-18 07:31:25 字数 727 浏览 1 评论 0原文

我以为我意识到了这种关系:在Python中一切都是对象,并且每个对象都有一个类型。但是课程呢?类是对象的蓝图,对象是类的实例。但我在Python中读过一篇文章 ,类本身就是对象。我认为一个对象如果没有它的蓝图——它的类就不可能存在。但是,如果类是一个对象,那么它如何存在呢?

>>> type.__bases__
(<class 'object'>,)
>>> int.__bases__
(<class 'object'>,)
>>> str.__bases__
(<class 'object'>,)

那么,object 类是每个对象的蓝图吗?

>>> type(str)
<class 'type'>
>>> type(int)
<class 'type'>
>>> type(type)
<class 'type'>

那么,类 type 是所有其他类型的蓝图吗?

type 本身就是一个对象。我无法理解这一点。我无法想象类是对象。

I thought that I realized this relationship: In Python everything is an object, and every object has a type. But what about classes? A class is a blueprint of an object, and an object is instance of a class. But I have read in an article that in Python, classes are themselves objects. I thought that an object cannot exist without its blueprint - its class. But, if class is an object, how it can exist?

>>> type.__bases__
(<class 'object'>,)
>>> int.__bases__
(<class 'object'>,)
>>> str.__bases__
(<class 'object'>,)

So, the class object is the blueprint of every object?

>>> type(str)
<class 'type'>
>>> type(int)
<class 'type'>
>>> type(type)
<class 'type'>

So, class type is blueprint of every other type?

But type is an object itself. I cannot understand this. I cannot imagine that classes are objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-10-25 07:31:25

在 Python 中,所有可以命名的东西都是对象——包括函数、类和元类。每个对象都有一个关联的类型(这是同一事物的两个名称——“类型”和“类”在Python 3中是相同的)。该类型本身又是一个对象,并且它本身有一个关联的类型。类型的类型称为元类(当然,它同样可以称为元类型,但不使用后一个词)。您可以使用type()来确定对象的类型。如果你迭代地查询一个对象的类型、其类型的类型等等,你总是会在某个时刻得到类型 type ,通常是在两个步骤之后:

type(3)    # --> int
type(int)  # --> type
type(type) # --> type

另一个例子,使用“meta -metaclasses":

class A(type):
    pass
class B(type, metaclass=A):
    pass
class C(metaclass=B):
    pass
c = C()

type(c)    # --> C
type(C)    # --> B
type(B)    # --> A
type(A)    # --> type
type(type) # --> type

type 本身就是 type 类型,这并不矛盾。

Everything that can be given a name in Python is an object - including functions, classes and metaclasses. Every object has an associated type or class (these are two names for the same thing -- "type" and "class" are the same in Python 3). The type itself is an object again, and has itself an associated type. The type of a type is called a metaclass (of course, it could equally well be called a metatype, but the latter word is not used). You can use type() to determine the type of an object. If you iteratively query the type of an object, the type of its type and so on, you will always end up with the type type at some point, usually after two steps:

type(3)    # --> int
type(int)  # --> type
type(type) # --> type

Another example, using "meta-metaclasses":

class A(type):
    pass
class B(type, metaclass=A):
    pass
class C(metaclass=B):
    pass
c = C()

type(c)    # --> C
type(C)    # --> B
type(B)    # --> A
type(A)    # --> type
type(type) # --> type

There is no contradiction in type being itself of type type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文