46267

Python; name bindings are not object references?

I am trying to understand what exactly a Python name binding is, and when this binding is interpreted.

In c,

include <stdio.h> int main() { int X = 42; int* Y[1]; Y[0] = &X; X = 666; printf("%d", *Y[0]); return 0; }

prints 666. I was expecting the block of Python code:

X = 42 L = [] L.append(X) #3 X = 666 print(L) #5

to do the same, but it does not. What exactly happens between the lines labeled 3 and 5? Does #3 make another reference to the object known as "42", like X, lets call it X', and store X' in the object pointed to by L, which is []?

Answer1:

What you state is almost what happens:

X = 42 # Create new object 42, bind name X to it. L = [] L.append(X) # Bind L[0] to the 42 object. X = 666 # Create new object 666, bind name X to it. print(L) # Will not see the 666.

The append is not binding the array element to the X, it's binding it to the object behind X, which is the 42.

When I first realised this is the way Python worked, things (specifically, things like this which had previously confused me and caused much angst and gnashing of teeth) became so much clearer.

Recommend

  • Change iframe source from link href
  • save a juptyer notebook with specific name within the code
  • Why does ANTLR require all or none alternatives be labeled?
  • Excel VBA Intersect
  • Recursion Control flow
  • How to get the index of element in the List in c#
  • Avoid Inheriting Super Class Tests in ScalaTest
  • how to set to NULL all the filestream varbinary(max) fields?
  • Responsive left sidebar open close
  • Why cout is producing no output on Code Blocks?
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • SyntaxError: (irb):26: both block arg and actual block given
  • How do I formally document a C# Attribute in UML?
  • In-place sed command not working
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Appending Character to Character Array In C
  • Can I check if a recipient has an automatic reply before I send an email?
  • R - Combining Columns to String Based on Logical Match
  • Read text file and split every line in MSBuild
  • Delete MySQLi record without showing the id in the URL
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How to pass list parameters for each object using Spring MVC?
  • how to add data labels for bar graph in matlab
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • need help with bizarre java.net.HttpURLConnection behavior
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?