Most voted "python-internals" questions
Use this tag only when the question is related to some internal Python behavior.
Learn more…18 questions
Sort by count of
-
17
votes2
answers653
viewsHow does Python treat the "Yield" command internally?
Was reading on command yield from Python, and it seems to me that this command creates a Generator which would be a kind of data list in which the return on value occurs over demand, as if the last…
-
13
votes2
answers265
viewsWhen is a default argument evaluated in Python?
Let us consider the following class: class Foo: def __init__(self, values = []): self.values = values Note that we are using an instance attribute and not a class attribute, but see the following…
-
13
votes2
answers262
viewsCheck "in" return change the Python result
It is known that to check whether a particular item does not belong to a list it is sufficient to use the operator in: values = [1, 2, 3, 4, 5] if 9 not in values: print("9 não pertence à lista.")…
-
12
votes2
answers1007
viewsWhat is Ellipsis in Python?
In the list of native constants from Python, you can find Ellipsis. print(Ellipsis, type(Ellipsis)) #-> (Ellipsis, <type 'ellipsis'>) In Python 3, there is still syntactic sugar ... that…
-
12
votes1
answer161
viewsWhy does 2*i*i tend to be faster than 2*(i*i) when i is integer?
The two multiplications, 2*i*i and 2*(i*i), are equal and must generate the same result, what changes is only the order that the multiplications are made, but apparently are treated differently by…
-
9
votes2
answers643
viewsHow does the int function handle the n character?
I created a list: usuarios = ['123\n','123\n4'] I tried to turn the index 0 into integer using the int() int(usuarios[0]) Upshot: 123 But when I tried to do the same with index 1: int(usuarios[1])…
-
9
votes2
answers102
viewsWhy should descriptors instances in Python be class attributes?
I’m studying descritores in Python and I found that they should be implemented as atributos de classe for example: class Descriptor: def __init__(self, obj): self.obj = obj def __get__(self,…
-
9
votes3
answers566
viewsHow does Python handle and represent an array internally?
In Python any type of array is of class type list, see: array = ['Gato', 'Jake', 'Finn'] print(type(array)) Exit: <class 'list'> That is, every array is an object of list. However, there is…
python array characteristic-language python-internals cpythonasked 5 years, 10 months ago gato 22,329 -
8
votes1
answer147
viewsWhy do these two ways of initializing the same list in Python generate structures of different sizes?
It is common to need to initialize a list in Python with a defined amount of elements and we can do this in two ways: 1) multiplying the list with an element by the desired amount; or 2) using the…
-
8
votes2
answers118
viewsWhy does multiple variable assignment create a list when used with the asterisk operator?
Suppose there is a tuple with three values, ('a', 1, 'b'), and you want to assign the first value in one variable and keep the rest in another. For this you can do: a, *b = ('a', 1, 'b') Thus, a…
-
7
votes2
answers770
viewsWhat is the difference between namedtuple and Namedtuple?
To module documentation typing states that the two code snippets below are equivalent. Using typing.NamedTuple: from typing import NamedTuple class Employee(NamedTuple): name: str id: int Using…
-
7
votes2
answers707
viewsAre class methods recreated for each instance in Python?
By what I observed when generating an instance of a class all class methods are recreated in a different memory position as in the excerpt below: class Foo(): def __init__ (self): self.x = 10 def…
-
6
votes2
answers208
viewsWhat is the actual implementation of "in" in Python?
This week I found myself wondering what the actual implementation of "in" in Python is. To try to answer, I went to look the official documentation about his details, and only say that: For…
-
6
votes1
answer669
viewsWhy do formatting options not work with lists, dictionaries, and other objects?
When I want to print a number or string, I can use f-strings (in Python >= 3.6) or str.format, and I can only pass the variable between keys, or use the formatting options. Ex: numero, texto =…
python python-3.x characteristic-language python-internals cpythonasked 4 years, 7 months ago hkotsubo 55,826 -
5
votes1
answer742
viewsHow does Python manage memory when assigning different types?
I wanted to understand how dynamic typing is done. In Python, for example, when we create a variable with content a number int and then that same variable gets a string, the fact of not giving…
-
2
votes1
answer704
viewsHow does Python handle common functions and internal Python functions?
I’m looking at Python’s dual functions compared to the common functions that use def to create them. See an example of a common function that converts a number to binary: def…
python function characteristic-language lambda-expressions python-internalsasked 5 years, 9 months ago gato 22,329 -
1
votes0
answers48
viewsWhat is the relationship and differences between Process and Thread?
I’m reading regarding the class Process and in the documentation it says that it follows the implementation of Threads, however, I have a question regarding the process class and threads. Doubts How…
-
1
votes1
answer85
viewsWhat is the purpose of the "Pyobject" structure and what are the objectives of its members?
I’m analyzing the structure Pyobject of Cpython. The code for this structure follows below. Structure PyObject: typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; struct _typeobject…