简体   繁体   中英

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. The question is: is the BlaBlaCar class an object too so it is an object on it is own when I not call it? Does it exist in the memory as an object when is defined as 'class BlaBlaCar(object)' ???

EDIT: It is clear to me that when I do:

print(BlaBlaCar()) then I instantiate class object.

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 . Almost everything in Python is an Object. I looked up some references for this as well.

Here, in the Python Docs, it is stated:

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. (You could conceivably place a class definition in a branch of an if statement, or inside a function.)

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).

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. What's an Object? Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute doc , which returns the doc string defined in the function's source code. The sys module is an object which has (among other things) an attribute called 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; 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). 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).

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. 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. 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. But it is important to note that

  1. the object which is created (the class itself) is an instance of type .
  2. no instance of the class ( BlaBlaCar in your example) is created at that time.

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.

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__ ). 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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