Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

variables - Understanding python's name binding

I am trying to clarify for myself Python's rules for 'assigning' values to variables.

Is the following comparison between Python and C++ valid?

  1. In C/C++ the statement int a=7 means, memory is allocated for an integer variable called a (the quantity on the LEFT of the = sign) and only then the value 7 is stored in it.

  2. In Python the statement a=7 means, a nameless integer object with value 7 (the quantity on the RIGHT side of the =) is created first and stored somewhere in memory. Then the name a is bound to this object.

The output of the following C++ and Python programs seem to bear this out, but I would like some feedback whether I am right.

C++ produces different memory locations for a and b while a and b seem to refer to the same location in Python (going by the output of the id() function)

C++ code

#include<iostream>
using namespace std;
int main(void)
{
  int a = 7;
  int b = a; 
  cout << &a <<  "  " << &b << endl; // a and b point to different locations in memory
  return 0;
}

Output: 0x7ffff843ecb8 0x7ffff843ecbc

Python: code

a = 7
b = a
print id(a), ' ' , id(b) # a and b seem to refer to the same location

Output: 23093448 23093448

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you're basically correct. In Python, a variable name can be thought of as a reference to a value (not in terms of a C++ reference, though that works similarly, more just stating that it refers to something).

As an aside, the Python way is very similar to the C++ int &b = a, which just means a and b refer to the same value.

Or the C int *pb = &a, which means a and *pb refer to the same value, but with all the confusion that brings to people who haven't yet accepted the brain-bendedness of C :-)

Assigning to the variable name in Python makes the name refer to a different value, it never copies the value itself:

a = 7   # Create "7", make "a" refer to it.
b = a   # make "b" refer to  the "7" as well.
a = 42  # Create "42", make "a" refer to it, b still refers to the "7".

(I say "create" but that's not necessarily so - if a value already exists somewhere, it may re-use it).

In a C-like language, that second statement b = a creates a new value, copies the "7" into it and then names that b. In Python, it simply ends up with a and b referring to the same value.

Where the underlying data is immutable (cannot be changed), that usually makes Python look as if it's behaving identically to the way C does it.

But, for mutable data (same as using pointers in C or references in C++), people can sometimes be surprised because they don't realise that the value behind it may be shared:

>>> a = [1,2,3] ; print a
[1, 2, 3]

>>> b = a ; print b
[1, 2, 3]

>>> a[1] = 42 ; print a
[1, 42, 3]

>>> print b   #WTH?
[1, 42, 3]

There are ways to get independent copies of a value, with things such as:

b = a[:]
b = [item for item in a]

(which will work to one level, where b = a works to zero levels), or using deepcopy if you want if totally unique, to whatever level is necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...