Confused in name binding in python

  • Context: Python 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confused Python
Click For Summary
SUMMARY

The discussion centers on the concept of name binding in Python, specifically how variable assignments link names to objects. The user illustrates this with examples, such as assigning values to variables and how they reference the same object in memory. The example function provided, my_sum(lst), demonstrates practical use by summing a list of numbers, highlighting the importance of understanding name binding for effective programming. The user seeks further clarification through visual aids and practical applications of this concept.

PREREQUISITES
  • Understanding of Python variable assignment
  • Basic knowledge of Python functions
  • Familiarity with Python data types (e.g., integers, lists)
  • Concept of object references in Python
NEXT STEPS
  • Study Python's memory management and object references
  • Learn about Python's variable scope and lifetime
  • Explore visual tools for understanding Python variable binding
  • Investigate advanced topics like closures and decorators in Python
USEFUL FOR

Python developers, educators teaching programming concepts, and anyone looking to deepen their understanding of variable management and memory in Python.

shivajikobardan
Messages
637
Reaction score
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
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K