0

I have read multiple textbooks, articles and watched multiple videos about name binding in python.

Till now what I understand can be summarized in this-:

x=1 means name x is binded to object "1"

z=x and we know x=1

=> z=1 so z=x means z is binded to object 1

y=2
x=y

name y binds to object "2"

name x binds to object "2"

This is all I understand about name binding. I can't see how this simple concept can have any use in programming. This looks like math to me.

  1. I need 1 example program to understand things I asked here.

  2. I need 1 application of this concept

  3. I need a figure depicting what is exactly happening when we declare variable x=1 and when we later do x=5 then we do y=2 then x=y. What is happening inside the system? I want that with figures.

ladhee
  • 71
  • 5

1 Answers1

1

Programming is different from algebra, as you noted. Here is an example of the difference between the two :

x = 1
y = x
x = x + 1
y = x / 4
print(x)  # 2
print(y)  # 0.5

A program is a sequence of instructions, which modify the computer state. In regular math you can not say x = x + 1, it is nonsensical. But between two lines of a program, time has passed, state has changed. You can say that x equals some thing at one time, and some different thing at another time.

But programs are abstractions. The Python code you write with human letters will get compiled into the Python bytecode, which will get interpreted to actual instructions, which will be transmitted from the RAM your userland process allocated down to the processor core, which will transform, decode and execute it (I skipped many layers and details). Ultimately, when you code in Python, you are sort of guiding your processor from a long (metaphorical) distance. What name binding means at the Python language levels is based on how things work way down there, in the processor. There exist some registers, some operations to move data to/between/from them and the (layers of cache before the) RAM.
Python is not the best language to apprehend all of this, lower-level languages are preferred. The middle ground is the C language, but the reasonable best is Assembly.
There is no name binding at the metal level, it is instead a functionnality offered by most of the programming languages because most humans prefer to have them. But it's just for convenience. How it works depends of the language.

In Python, assignation is a bit tricky because it hides whether it is done by copy or by reference. That's the cause of this gotcha. If you want to understand how name binding works, I recommend you study the Python Data Model.
Also, there seem to be StackOverflow questions related to what you want, see for example this one.

I did not follow the instructions on how to answer your question because I think it does not respect the rules.

Lenormju
  • 4,078
  • 2
  • 8
  • 22