理解python的名称绑定 [英] Understanding python's name binding

查看:29
本文介绍了理解python的名称绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为自己澄清 Python 的赋值"规则到变量.

以下 Python 和 C++ 的比较是否有效?

  1. 在 C/C++ 中,语句 int a=7 表示,内存分配给名为 a 的整数变量(LEFT 上的数量= 符号)然后才将值 7 存储在其中.

  2. 在 Python 中,语句 a=7 表示一个 无名 值为 7 的整数对象(右侧=) 首先创建并存储在内存中的某处.然后名称 a 绑定到这个对象.

以下 C++ 和 Python 程序的输出似乎证实了这一点,但我想知道我是否正确.

C++ 为 ab 生成不同的内存位置而 ab 在 Python 中似乎指的是同一个位置(通过 id() 函数的输出)

C++ 代码

#include使用命名空间标准;int main(void){int a = 7;int b = a;cout<<&a <<" " <<&b<<结束;//a 和 b 指向内存中的不同位置返回0;}

输出:0x7ffff843ecb8 0x7ffff843ecbc

Python:代码

a = 7b = aprint id(a), ' ' , id(b) # a 和 b 似乎指向同一个位置

输出:23093448 23093448

解决方案

是的,您基本正确.在 Python 中,变量名可以被认为是对值的引用(不是 C++ 引用,虽然工作原理类似,更只是说明它引用了某物).

<块引用>

顺便说一句,Python 的方式与 C++ int &b = a 非常相似,仅表示 ab引用相同的值.

或者 C int *pb = &a,这意味着 a*pb 引用相同的值,但所有给尚未接受 C 的大脑弯曲的人带来的困惑:-)

在 Python 中为变量名赋值会使名称引用一个不同的值,它永远不会复制值本身:

a = 7 # 创建7",生成a";参考它.b = a # 制作b";参考7"以及.a = 42 # 创建42",制作a";指它,b仍指7".

(我说的是创建",但不一定如此——如果某个值已经存在于某处,它可能会重复使用它).

在类 C 语言中,第二个语句 b = a 创建一个 值,复制7"进入它,然后将其命名为 b.在 Python 中,它只是以 ab 结束,引用 same 值.

在底层数据是不可变的(无法更改)的情况下,这通常会使 Python 看起来好像它的行为与 C 的行为方式相同.

但是,对于可变数据(与在 C 中使用指针或在 C++ 中使用引用相同),人们有时会感到惊讶,因为他们没有意识到其背后的价值可能是共享的:<预><代码>>>>a = [1,2,3];打印一个[1, 2, 3]>>>乙 = ;打印 b[1, 2, 3]>>>[1] = 42 ;打印一个[1, 42, 3]>>>打印 b #WTH?[1, 42, 3]

有多种方法可以获取独立值的副本,例如:

b = a[:]b = [a 中项目的项目]

(将工作到一个级别,其中 b = a 工作到零级别),或使用 deepcopy 如果你想要完全唯一,到任何必要的级别.

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

解决方案

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.

这篇关于理解python的名称绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆