D
D
Daniil Kotlyar2021-06-21 20:30:29
Python
Daniil Kotlyar, 2021-06-21 20:30:29

Is the string initialized at the time of the error?

Good evening, tell me the answer to this question :)

I have the following piece of code:

int_text = int(input("it's just an instance, bro"))


If I enter nothing, then Value Error for int() with base 10 will pop up: " "

So, at the time of the error, did the int_text variable already assign an empty string to itself?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alan Gibizov, 2021-06-21
@Daibend

As I understand it, how "initialization" happens.
An object is created somewhere, and an int_text variable is created and "attached" to that object, like a tag on a tea bag.
And the “empty string” object is not created here. Well, more precisely, it does not work out at the output of the right side of the "equation". And the label int_text is not attached anywhere. And, apparently, it is immediately eaten by the garbage collector.

M
MinTnt, 2021-06-21
@MinTnt

If we consider everything not as an assignment, but as a "binding" of a variable name with an object.
And submit the assignment record itself in a slightly different form.

globals().__setitem__('int_text', int(input("it's just an instance, bro")) )

It is possible to present everything as a function to which the necessary attributes are transferred.
Those. the name of the variable that we passed, as well as the object that we want to assign, are at first only at the local level of this very function, until it fits into the list of global variables.
That is, if everything is drawn in a certain sequence of actions.
First, the objects that are passed to the function are initialized.
1) The attribute is passed as a string object - accepted
2) The attribute is passed as a function - waiting for the result. As soon as it receives the result from the functions, it will go further, or it will not go in case of an error. And the assignment function will run.
3). In the mechanics of an assignment function, a variable name is associated with an object already served on a silver platter.
But with functions containing recursion, everything is more interesting. For example, if you write x = x, it will not work to call recursion, since you first need to get the x object. But if you write a function, for example x = lambda: x(). The system can immediately get a ready function object. And since its contents are "initialized" only when used directly. When we already use the function itself by the name of the variable, then when the function encounters x () during execution, it freely executes it, since the variable name has already been created at the local level, and is associated with the object.
And only after the desired object is completely ready, it will associate the variable with it.
Ps It doesn't really fit with reality (I mean the representation of everything as a function), but the mechanics are similar. It's just easier for me to represent everything as a function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question