Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 653
Dariusz Krynicki Avatar asked Aug 07 '26 08:08

Dariusz Krynicki


2 Answers

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.
like image 189
jacg Avatar answered Aug 09 '26 00:08

jacg


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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!