简体   繁体   English

Python类作为对象

[英]Python class as object

I wonder if the class below becomes an object in a moment when is defined or does it become and object in the moment when I instantiate it? 我想知道下面的类是否在一个定义的时刻成为一个对象,或者在我实例化它的那一刻它是否成为对象?

class BlaBlaCar(object):
    def __init__(self):
        pass
    def bla(self):
        pass

blabla = BlaBlaCar()

so, blabla is an object. 所以,blabla是一个对象。 The question is: is the BlaBlaCar class an object too so it is an object on it is own when I not call it? 问题是:BlaBlaCar类是否也是一个对象,所以当我不调用它时它是一个自己的对象? Does it exist in the memory as an object when is defined as 'class BlaBlaCar(object)' ??? 当它被定义为'class BlaBlaCar(object)'时它是否作为对象存在于内存中?

EDIT: It is clear to me that when I do: 编辑:当我这样做时,我很清楚:

print(BlaBlaCar()) then I instantiate class object. print(BlaBlaCar())然后我实例化类对象。

The question is when I create the class body definition do I create object to? 问题是当我创建类体定义时,我是否创建了对象?

I concur with @jonrsharpe 's comment and I think you can find all information you need here . 我同意@jonrsharpe的评论,我想你可以在这里找到你需要的所有信息。 Almost everything in Python is an Object. Python中几乎所有东西都是Object。 I looked up some references for this as well. 我也查了一些参考文献。

Here, in the Python Docs, it is stated: 这里,在Python Docs中,它声明:

Class Definition Syntax The simplest form of class definition looks like this: 类定义语法最简单的类定义形式如下所示:

class ClassName:
    <statement-1>
     .
     .
     .
    <statement-N>

Class definitions, like function definitions (def statements) must be executed before they have any effect. 类定义,如函数定义(def语句)必须在它们产生任何影响之前执行。 (You could conceivably place a class definition in a branch of an if statement, or inside a function.) (您可以想象将类定义放在if语句的分支中,或者放在函数内部。)

In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful — we'll come back to this later. 实际上,类定义中的语句通常是函数定义,但其他语句是允许的,有时也很有用 - 我们稍后会再回过头来讨论它。 The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods — again, this is explained later. 类中的函数定义通常具有一种特殊形式的参数列表,由方法的调用约定决定 - 再次,这将在后面解释。

When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. 输入类定义时,将创建一个新的命名空间,并将其用作本地范围 - 因此,对局部变量的所有赋值都将进入此新命名空间。 In particular, function definitions bind the name of the new function here. 特别是,函数定义在此处绑定新函数的名称。

When a class definition is left normally (via the end), a class object is created . 正常保留类定义(通过结尾)时,会创建一个类对象 This is basically a wrapper around the contents of the namespace created by the class definition; 这基本上是类定义创建的命名空间内容的包装器; we'll learn more about class objects in the next section. 我们将在下一节中详细了解类对象。 The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header (ClassName in the example). 恢复原始本地范围(在输入类定义之前生效的范围),并且类对象在此绑定到类定义标头中给出的类名(示例中为ClassName)。

From this you take, that the pure definition of the class is an object. 从中你可以看出,类的纯粹定义是一个对象。

And here I could find this information about objects: 在这里,我可以找到有关对象的信息:

2.4.2. 2.4.2。 What's an Object? 什么是对象? Everything in Python is an object, and almost everything has attributes and methods. Python中的所有东西都是一个对象,几乎所有东西都有属性和方法。 All functions have a built-in attribute doc , which returns the doc string defined in the function's source code. 所有函数都有一个内置属性doc ,它返回函数源代码中定义的doc字符串。 The sys module is an object which has (among other things) an attribute called path. sys模块是一个对象,其中包含一个名为path的属性(除其他外)。 And so forth. 等等。

Still, this begs the question. 不过,这引出了一个问题。 What is an object? 什么是对象? Different programming languages define “object” in different ways. 不同的编程语言以不同的方式定义“对象”。 In some, it means that all objects must have attributes and methods; 在某些情况下,这意味着所有对象必须具有属性和方法; in others, it means that all objects are subclassable. 在其他情况下,这意味着所有对象都是可子类化的。 In Python, the definition is looser; 在Python中,定义更宽松; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). 一些对象既没有属性也没有方法(第3章中有更多内容),并且并非所有对象都是可子类化的(在第5章中有更多内容)。 But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4). 但是在某种意义上,一切都是一个对象,它可以被赋值给一个变量或作为一个参数传递给一个函数(在第4章中更多)。

This is so important that I'm going to repeat it in case you missed it the first few times: everything in Python is an object. 这非常重要,如果你错过了前几次我会重复它:Python中的所有内容都是一个对象。 Strings are objects. 字符串是对象。 Lists are objects. 列表是对象。 Functions are objects. 功能是对象。 Even modules are objects . 甚至模块都是对象

Be aware, however, that no instance is created with the class definition until: 但请注意,在以下情况下,不会使用类定义创建实例:

blabla = BlaBlaCar()

As has been pointed out elsewhere, almost everything in Python is an object. 正如其他地方所指出的,Python中几乎所有东西都是对象。 However, I feel that your question 但是,我觉得你的问题

The question is when I create the class body definition do I create object to? 问题是当我创建类体定义时,我是否创建了对象?

has not been addressed directly yet. 尚未直接解决。

When Python executes the definition of the class, yes, an object is created. 当Python执行类的定义时,是的,创建了一个对象。 But it is important to note that 但重要的是要注意到这一点

  1. the object which is created (the class itself) is an instance of type . 创建的对象(类本身)是type的实例。
  2. no instance of the class ( BlaBlaCar in your example) is created at that time. 此时没有创建类的实例(在您的示例中为BlaBlaCar )。

First, to dispell any doubt, a class definition does give you an object representing the class itself (not an instance): 首先,为了消除任何疑问,类定义确实为您提供了一个表示类本身(而不是实例)的对象:

>>> class BlaBlaCar(object):
...     pass
...
>>> id(BlaBlaCar) 
57088312L

So you see BlaBlaCar is an object in memory. 所以你看到BlaBlaCar是内存中的一个对象。

You ask, "where is the class object created to?" 你问,“创建的类对象在哪里?”

The class definition normally is in a module (or in the example above, in the current namespace, which is __main__ ). 类定义通常位于模块中(或者在上面的示例中,在当前命名空间中,即__main__ )。 When this module is executed, a module object is created. 执行此模块时,将创建模块对象。 As part of module execution, any class definition executed will result in a class object which is then bound to the class name in the module's namespace. 作为模块执行的一部分,执行的任何类定义都将生成一个类对象,然后将其绑定到模块命名空间中的类名。

So in a sense, you can say the class object is "in" the module's namespace. 所以从某种意义上说,你可以说类对象是“在”模块的命名空间中。 But really, you can ask a similar question, "where is the module object created to?", at which point, the answer just is that the interpreter creates the object and has one or more references to it, just like any other object you create. 但实际上,您可以问一个类似的问题,“ 模块对象在哪里创建?”,此时答案就是解释器创建对象并有一个或多个引用,就像任何其他对象一样创造。

A subtlety is that the garbage collector will collect any object that doesn't have a reference to it. 一个微妙之处在于垃圾收集器将收集任何没有引用它的对象。 In the case of the class object, the surrounding module has it in its namespace. 对于类对象,周围的模块在其命名空间中具有它。 In the case of the module itself, there is always a reference from places like sys.modules . 对于模块本身,始终存在来自sys.modules类的sys.modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM