Python 的布尔值是按值传递的吗?

发布于 2024-08-06 18:53:34 字数 120 浏览 8 评论 0原文

我发送了对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下伊人醉 2024-08-13 18:53:34

Python 变量不是 C++ 意义上的“引用”。相反,它们只是绑定到内存中某个任意位置的对象的本地名称。如果该对象本身是可变的,则对其进行的更改将在已将名称绑定到该对象的其他作用域中可见。许多基本类型(包括 boolintstrtuple)都是不可变的。 > 然而。您无法就地更改它们的值;相反,您可以为本地范围内的同名分配一个新值。

事实上,几乎任何时候*您看到 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, and tuple) 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 name foo is being assigned a new value (X) within your current local namespace, not that a location in memory named by foo is having its internal pointer updated to refer instead to the location of X.

*- 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 like obj.setFoo(X).

古镇旧梦 2024-08-13 18:53:34
  • TrueFalse 是单例对象。

True 只不过是一个标签,指向内存中某个 bool 类型的对象,位于某个内存地址,内部值为整数 1。 False 是间隔值为 0 的同一事物。

在此处输入图像描述

因为它们是单例对象,所以它们在程序的整个生命周期中将始终保留相同的内存地址。

  • Python 有一个 bool 类,用于表示布尔值。然而,bool 类是 int 类的子类。由于 bool 对象也是 int 对象,因此它们也可以解释为整数 1 和 0

    int(True) ->; 1
    int(假) ->0
    

重要的是要理解,True 和 1 不同对象。 True 位于某个内存地址,` 位于某个不同的内存地址。我们在这里比较它们的引用:

  id(True) != id(1)

或者

  True is 1 -> False

因为恒等运算符检查内存地址

  • 传递值

    <前><代码>真>假-->真的

这是奇怪的行为。在这种情况下,由于 True 保留 1,False 保留 0,

 1 > 0 

另一个例子

   a=True+True+True

a 将是 3。我们传递值似乎是正确的,但所有内容都是通过引用传递的 - 只是对于单例对象来说,它是相同的引用。

  • True and False 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.

enter image description here

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. Since bool objects are also int objects, they can also be interpreted as the integers 1 and 0

    int(True)   -> 1
    int(False) ->0
    

IMPORTANT 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:

  id(True) != id(1)

Or

  True is 1 -> False

Because identity operator checks the memory address

  • Passing values

     True > False -->  True
    

This is strange behavior. In this case, since True holds 1 and False holds 0

 1 > 0 

another example

   a=True+True+True

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.

半枫 2024-08-13 18:53:34

这取决于对象是可变的还是不可变的。不可变对象的行为就像您在 bool 中看到的那样,而可变对象会发生变化。

供参考:http://www.testingreflections.com/node/view/5126

Python 按值传递对象引用(如 Java),Python 中的所有内容都是对象。这听起来很简单,但是您会注意到某些数据类型似乎表现出按值传递特征,而另一些数据类型似乎表现出按引用传递......这是怎么回事?

了解可变和不可变对象非常重要。有些对象,如字符串、元组和数字,是不可变的。在函数/方法内部更改它们将创建一个新实例,并且函数/方法外部的原始实例不会更改。其他对象(例如列表和字典)是可变的,这意味着您可以就地更改对象。因此,改变函数/方法内部的对象也会改变外部的原始对象。

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 passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?

It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.

只有影子陪我不离不弃 2024-08-13 18:53:34

需要记住的是,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.

蓝眸 2024-08-13 18:53:34

简而言之,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文