Confused in name binding in python

In summary, Name binding in Python is the process of binding a name to an object. This can be seen in the example of x=1, where the name x is bound to the object "1". This concept is useful in programming, such as in the example of the assignment statement x=e, which evaluates the expression e and stores its result in variable x. An example of this can be seen in the code for the function my_sum, where the name s is bound to the object 0 and is reassigned to the value of s+x in each iteration of the for loop. Another application of name binding is in the declaration of variables, such as in the case of x=y, where the name x is bound to the same
  • #1
shivajikobardan
674
54
(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
Assignment x = e evaluates the expression e and stores its result in variable x.

shivajikobardan said:
I can't see how this simple concept can have any use in programming.
Assignment is useful in the following code.

Python:
def my_sum(lst):
  s = 0
  for x in lst:
    s = s + x
  return s

>>> my_sum([1,2,3])
6
 

1. What is name binding in python?

Name binding in python is the process of assigning a name to a variable or object. It allows you to reference and manipulate the value of that variable or object using its name.

2. Why is name binding important in python?

Name binding is important in python because it allows for easier and more efficient coding. By assigning names to variables and objects, you can easily reference and use them throughout your code without having to remember their values or locations.

3. What is the difference between global and local name binding in python?

In python, global name binding refers to variables and objects that are accessible throughout the entire program. Local name binding, on the other hand, refers to variables and objects that are only accessible within a specific function or block of code.

4. How does name binding work in python?

In python, name binding works by creating a reference between a name and a value. When a name is assigned to a variable or object, that name becomes a reference to the value of that variable or object. This allows you to manipulate the value by using its assigned name.

5. What are some common mistakes when dealing with name binding in python?

Some common mistakes when dealing with name binding in python include using the wrong name when referencing a variable or object, not properly defining global and local variables, and not understanding the scope of variables within functions and blocks of code.

Similar threads

  • Programming and Computer Science
Replies
1
Views
981
  • Programming and Computer Science
Replies
7
Views
461
Replies
6
Views
639
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
889
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
989
Back
Top