Are Python's bools passed by value?

半世苍凉 提交于 2021-01-27 04:07:50

问题


I sent a reference to a bool object, and I modified it within a method. After the method finished it's 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?


回答1:


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).




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/1494696/are-pythons-bools-passed-by-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!