Python 的布尔值是按值传递的吗?
我发送了对 bool 对象的引用,并在方法中修改了它。方法执行完毕后,方法外的bool值没有变化。
这让我相信 Python 的 bool 是按值传递的。这是真的吗?还有哪些其他 Python 类型有这样的行为?
I sent a reference to a bool object, and I modified it within a method. After the method finished its execution, the value of the bool outside the method was unchanged.
This leads me to believe that Python's bools are passed by value. Is that true? What other Python types behave that way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 变量不是 C++ 意义上的“引用”。相反,它们只是绑定到内存中某个任意位置的对象的本地名称。如果该对象本身是可变的,则对其进行的更改将在已将名称绑定到该对象的其他作用域中可见。许多基本类型(包括
bool
、int
、str
和tuple
)都是不可变的。 > 然而。您无法就地更改它们的值;相反,您可以为本地范围内的同名分配一个新值。事实上,几乎任何时候*您看到
foo = X
形式的代码时,都意味着名称foo
被分配了一个新值 (X
code>) 在当前本地命名空间中,而不是内存中由foo
命名的位置正在更新其内部指针以引用X
的位置。*- Python 中唯一的例外是属性的 setter 方法,这可能允许您编写 obj.foo = X 并在后台重写它以调用像 obj 这样的方法.setFoo(X)。
Python variables are not "references" in the C++ sense. Rather, they are simply local names bound to an object at some arbitrary location in memory. If that object is itself mutable, changes to it will be visible in other scopes that have bound a name to the object. Many primitive types (including
bool
,int
,str
, andtuple
) are immutable however. You cannot change their value in-place; rather, you assign a new value to the same name in your local scope.In fact, almost any time* you see code of the form
foo = X
, it means that the namefoo
is being assigned a new value (X
) within your current local namespace, not that a location in memory named byfoo
is having its internal pointer updated to refer instead to the location ofX
.*- the only exception to this in Python is setter methods for properties, which may allow you to write
obj.foo = X
and have it rewritten in the background to instead call a method likeobj.setFoo(X)
.True
和False
是单例对象。True
只不过是一个标签,指向内存中某个 bool 类型的对象,位于某个内存地址,内部值为整数 1。False
是间隔值为 0 的同一事物。因为它们是单例对象,所以它们在程序的整个生命周期中将始终保留相同的内存地址。
Python 有一个
bool
类,用于表示布尔值。然而,bool 类是 int 类的子类。由于bool
对象也是int
对象,因此它们也可以解释为整数 1 和 0重要的是要理解,
True
和 1 不同对象。True
位于某个内存地址,` 位于某个不同的内存地址。我们在这里比较它们的引用:或者
因为恒等运算符检查内存地址
传递值
<前><代码>真>假-->真的
这是奇怪的行为。在这种情况下,由于 True 保留 1,False 保留 0,
另一个例子
a 将是 3。我们传递值似乎是正确的,但所有内容都是通过引用传递的 - 只是对于单例对象来说,它是相同的引用。
True
andFalse
are singleton objects.True
is nothing more than a label that points to some object in memory that is a bool type, lives at some memory address and it has internal value of integer 1.False
is the same thing which has an interval value of 0.Because they are singleton objects, they will always retain their same memory address throughout the lifetime of your program.
Python has
bool
class that used to represent Boolean values. However, the bool class is a subclass of the int class. Sincebool
objects are alsoint
objects, they can also be interpreted as the integers 1 and 0IMPORTANT to understand,
True
and 1 are not same objects.True
lives at some memory address and ` lives at some different memory address. we compare their references here:Or
Because identity operator checks the memory address
Passing values
This is strange behavior. In this case, since True holds 1 and False holds 0
another example
a will be 3. it seems right we are passing values but everything is passed by reference - it's just that for singleton objects it's the same reference.
这取决于对象是可变的还是不可变的。不可变对象的行为就像您在 bool 中看到的那样,而可变对象会发生变化。
供参考:http://www.testingreflections.com/node/view/5126
It depends on whether the object is mutable or immutable. Immutable objects behave like you saw with the bool, whereas mutable objects will change.
For reference: http://www.testingreflections.com/node/view/5126
需要记住的是,Python 中的函数或方法无法在调用命名空间中重新绑定名称。当您编写“我发送了对 bool 对象的引用,并在方法中修改了它”时,您实际上所做的(我猜测)是在内部重新绑定参数名称(bool 值通过调用绑定到该参数名称)方法体。
The thing to remember is that there is no way in Python for a function or a method to rebind a name in the calling namespace. When you write "I sent a reference to a bool object, and I modified it within a method", what you actually did (I am guessing) was to rebind the parameter name (to which the bool value was bound by the call) inside the method body.
简而言之,Python 中没有变量;有对象(如 True 和 False,布尔值恰好是不可变的)和名称。名称就是所谓的变量,但名称属于一个范围,通常不能更改本地名称以外的名称。
In short, there are no variables in Python; there are objects (Like True and False, the bools happen to be immutable), and names. Names are what you call variables, but names belong to a scope, you can't normally change names other than the local names.