• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Confused in name binding 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.
 

Ghost

Active Coder
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.

x = 1 # we are binding the variable x to the value of 1 (== 1, === 1)
x = 5 # Now x is no longer 1, it is 5
y = 2 # we are binding the variable y to the value of 2 (== 2, === 2)
x = y # Now x is no longer 5, it is the value of y (== 2, === 2, == y, ===y)

What is happening is this:
x = integer 1, Python says "do I have an object in memory to represent integer 1 - if so, use existing object"
So, x = 1 and y = 1, if you look for the specific object id with id(x) and id(y), they both are the same object ID.
This is the same as saying x == y or x is y


However, there is something to be said here about immutable vs mutable objects in Python.
If x is a list and y is a list, they can be ==, but an "is" comparison in Python will return false because [1,2] == [1,2] but [1,2] is not [1,2], as demonstrated below:
Perhaps this is what you are wanting to know more about. Immutables are saved in memory because 1 is always 1, and 2 is 2. A list on the other hand may change, so it's mutable. Below I wrote a script to demonstrate how you can evaluate values or the objects they are stored as.

Here are some tests you can run:

Python:
nums = [1,2,3]
numstwo = [1,2,3]
if nums == numstwo:
    # True, these are equal ==
    print("nums == numstwo")
   
if nums is numstwo: # False, nums is not numstwo
    print("nums is numstwo")
else:
    print("nums is not numstwo")
# This is because lists are "Mutable" and the "is / is not" in Python will not consider the two the same because their lists are stored as different objects in memory
# On the other hand, strings and integers are two types of immutable objects:
a = 1
b = 1
if a == b:
    # True, these are equal ==
    print("a == b (" + str(a) + ", " + str(b) + ")")
if a is b:
    # True, a is b
    print("a is b (" + str(a) + ", " + str(b) + ")")
id_a = id(a)
id_b = id(b)
if id_a == id_b:
    # True, same id in memory
    print("id_a == id_b (" + str(id_a) + ", " + str(id_b) + ")")
hex_a = hex(id_b)
hex_b = hex(id_b)
if hex_a == hex_b:
    # True, same id from memory in hexadecimal format
    print("hex_a == hex_b (" + hex_a + ", " + hex_b + ")")

c = "string"
d = "string"
if c == d:
    # True, these are equal ==
    print("c == d (" + c + ", " + d + ")")
if c is d:
    # True, c is d
    print("c is d (" + c + ", " + d + ")")
id_c = id(c)
id_d = id(d)
if id_c == id_d:
    # True, same id in memory
    print("id_c == id_d (" + str(id_c) + ", " + str(id_d) + ")")
hex_c = hex(id_c)
hex_d = hex(id_d)
if hex_c == hex_d:
    # True, same id from memory in hexadecimal format
    print("hex_c == hex_d (" + hex_c + ", " + hex_d + ")")

If you want to learn a bit more about Python objects, read this article:
 
Last edited:

Top