Confused about name binding in python

  • #1
shivajikobardan
543
34
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.
 

Answers and Replies

  • #2
pbuk
Science Advisor
Homework Helper
Gold Member
4,084
2,410
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.
 

Suggested for: Confused about name binding in python

  • Last Post
Replies
1
Views
415
Replies
3
Views
396
  • Last Post
Replies
2
Views
414
Replies
6
Views
628
  • Last Post
Replies
33
Views
2K
  • Last Post
Replies
4
Views
619
  • Last Post
Replies
1
Views
78
  • Last Post
Replies
9
Views
854
  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
24
Views
863
Top