Python Confused about name binding in python

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confused Python
AI Thread Summary
Name binding in Python involves associating variable names with objects in memory. When a variable is assigned, such as x=1, it binds the name x to the integer object 1. Subsequent assignments, like x=5 or y=2, update the bindings in the symbol table, which maps variable names to their respective memory locations. This concept is crucial for creating dynamic programs, allowing for user input and variable manipulation, as demonstrated in a simple addition program. Understanding name binding enhances comprehension of how Python manages memory and variable references, which is fundamental to programming in the language.
shivajikobardan
Messages
637
Reaction score
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
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
1
Views
1K
Replies
10
Views
3K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
4
Views
2K
Back
Top