-2

I would like to understand the data model in python (3.8.2), so maybe someone can explain in simple words, why every basic data type is a class-object and has so many attributes and functions.

Consider the following code:

num = 1337
type(num)  # <class 'int'>
dir(num)
>>> 
['__abs__',
 '__add__',
 '__and__',
...
 'real',
 'to_bytes']

So I can do:

num.__add__(1) 
>>> 1338

So my question is: why are such functions part of the user defined variables and isn't there quite some overhead to this?

Marcellvs
  • 391
  • 1
  • 3
  • 15
  • 1
    So you can define `+` and other operations for any class you want. (Yes, there are probably other ways to do it, but this is how Python does it.) Also, `int` in Python is not exactly a "basic" data type as `int` in C, as it can hold arbitrarily large values. – tobias_k Sep 18 '20 at 13:03
  • 1
    Where exactly do you see a memory overhead? I am not aware of methods costing a significant amount of memory in any language. The method is attached to the class, and it is a pointer to a function, so not too costly. There might be significant memory footprint in the implementation of the methods, but you cannot tell that this is the case from what you are showing. There is no extra memory cost associated to each individual object as far as I know. – norok2 Sep 18 '20 at 13:03

2 Answers2

1

Why so many methods:

Python is an object-oriented language. It's built on objects interacting with each other. Maybe there's no specific reason why Python has to be object-oriented, but the guy who wrote it gets to decide what kind of language it is and he chose this type.

In Python, the objects get to decide how an operation is implemented. That's why you can have natural syntax like 'str1' + 'str2' or 23 + 40. Note, these are both written in the same way, but will produce different results because the + operation is carried out on different objects.

What about memory management:

If the objects didn't decide how to implement an operation, the logic would still have to be stored somewhere. It would probably take up at least some memory.

The question is how much memory does an object use? To do this, you can use the sys.getsizeof function:

import sys
print(sys.getsizeof(int))  # Prints size in bytes
>>> 416
print(sys.getsizeof(1))
>>> 28

So, the memory of the class (int) is much larger than the object (1). This means that the instances of integers are much smaller than their classes, so memory doesn't inflate as drastically.

jrmylow
  • 679
  • 2
  • 15
1

"everything is object in Python", so "everything" (maybe not exactly everything, I don't know) is a class instance and has potentially attributes and methods.

That said you have to know that, by convention, every method whom name begins with __ is considered as private (and is almost really protected) and you should not use it directly.

Every method whom name starts and ends by __ is called a magic method and has a pretty well defined role. They could be considered as a sort of hooks bound to particular operators or moments in the object lifecycle.

For example __add__ is the method bound to the + operator. When you do an addition, you are actually calling the __add__ method of a number, giving it another number as parameter. As you noticed, you also can call directly the __add__ method if you want, but it is not encouraged.


Python is a high-level interpreted language. It provides many facilities and a clear syntax that allows to concentrate on real problems rather than implementation details.

Those facilities do not come from nowhere and are a benefit provided by (among others) an internal data model. This data model is certainly not as lightweight as a custom handcrafted C data structure specialy designed for a single problem but it is pratical and usable in many situations (and still reasonably lightweight and well performing for many usages).


If you are concerned by memory optimisation and extreme-performance, You probably should look some lower level languages like C or Go.

But keep in mind that solving any problem could then probably take much more time :)

Tryph
  • 5,946
  • 28
  • 49