Confused about name binding in python

In summary, name binding in Python refers to the process of creating a connection between a variable name and an object. This can be seen in examples such as x = 1, where x is bound to the object "1". This concept is commonly used in programming languages, including Python, to simplify code and make it more efficient. An example program is provided to demonstrate the use of variables in Python. In terms of what happens within the system when declaring and assigning variables, this involves parsing the code and creating an abstract syntax tree, followed by creating bytecode and executing it through a virtual machine. Other languages may have different processes, such as JIT compilation.
  • #1
shivajikobardan
674
54
TL;DR Summary
name binding concept in python
(I Didn't use code formatting here as I felt it was not necessary)

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
then,

y=2 #name y binds to object "2"
x=y #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.
 
Technology news on Phys.org
  • #2
I'm going to tackle 1 and 2 together.

Here is a program that adds 2 and 2:
Python:
print(2 + 2)

That's great, but if I want to add 2 and 3 I would need to write another program:
Python:
print(2 + 3)

Instead we use variables (almost all computer languages have this concept).
Python:
x = int(input())
y = int(input())

print(x + y)
Now I can enter any two integers I want and the program will print the sum.

You don't need to know the answer to 3 to write programs in Python, but since you asked, this is what Python effectively does:
  1. Parse the code x = 1 and translate it into the following actions:
    1. save a pattern to memory in the form of an integer object with the value 1
    2. create an entry in the symbol table mapping the variable name x to the memory location of the object above
  2. Parse the code x = 5 and translate it into the following actions:
    1. save a pattern to memory in the form of an integer object with the value 5
    2. change the entry for x in the symbol table remapping it to the memory location of the new object above
  3. Parse the code y = 2 and translate it into the following actions:
    1. save a pattern to memory in the form of an integer object with the value 2
    2. create an entry in the symbol table mapping the variable name y to the memory location of the object above
  4. Parse the code x = y and translate it into the following action:
    1. look up y in the symbol table and copy it's memory location to the memory location of x
Note that for performance reasons, Python in its most common implementation (CPython) doesn't do these things in the order I set them out: it does all of the parsing at once creating an abstract syntax tree AST, and then works through the AST creating bytecode which is simply a digital representation of the instructions I wrote above, finally it launches a form of virtual machine (the Python runtime) which executes the bytecode.

Finally note that only truly interpreted languages work the way I first wrote above: in 2021 the only significant such languages are bash scripts (posix)/batch files (windows). In between this and object code compilers such as C++ there is a range from languages that only create an AST (such as Lisp in most implementations) to just in time (JIT) compilers such as Google's V8 engine for JavaScript which turn the byte code into machine level instructions.
 
  • Like
Likes sysprog

FAQ: Confused about name binding in python

What is name binding in Python?

Name binding in Python refers to the process of assigning a name to a variable or object. It is essentially creating a reference to a specific value or data, which can then be accessed by using the assigned name.

How does name binding work in Python?

In Python, name binding works by creating a reference between a name and a value. When assigning a name to a variable or object, the name is bound to the value that it is referencing. This allows for the value to be easily accessed and manipulated using the assigned name.

What is the difference between global and local name binding in Python?

In Python, global name binding refers to assigning a name to a variable or object in the global scope, making it accessible throughout the entire program. Local name binding, on the other hand, refers to assigning a name within a specific function or block of code, making it only accessible within that function or block.

What happens if a variable is assigned a new value in Python?

If a variable is assigned a new value in Python, the name is rebound to the new value. This means that the variable will now reference the new value instead of the previous one. This is a common source of confusion for beginners, as they may expect the original value to still be referenced by the variable.

How can I avoid confusion with name binding in Python?

To avoid confusion with name binding in Python, it is important to have a clear understanding of the scope of variables and objects in your code. It is also helpful to use descriptive and meaningful names for your variables, to avoid accidentally reassigning a name to a different value. Additionally, using comments and proper indentation can also help make your code more readable and easier to understand.

Similar threads

Replies
1
Views
860
Replies
10
Views
2K
Replies
9
Views
2K
Replies
5
Views
3K
Replies
5
Views
2K
Replies
5
Views
1K
Replies
3
Views
975
Replies
4
Views
1K
Back
Top