tags:

views:

113

answers:

4

IMO python is pass by value if the parameter is basic types, like number, boolean

func_a(bool_value):
    bool_value = True

Will not change the outside bool_value, right?

So my question is how can I make the bool_value change takes effect in the outside one(pass by reference?

+1  A: 

Python does pass all values by reference, however, strings and numeric types are immutable. Therefore if you modify the value in the function it won't be reflected changed in the function argument.

You could always return a tuple instead of just a single value. ie:

return re_val,True

or if you want to pass a basic type by reference you could wrap it into a map?

map = {"some_bool":False}
func_a(map)

You could then modify the map, which is a mutable type.

GWW
+2  A: 

Unmutable types can't, but if you send a user-defined class instance, a list or a dictionary, you can change it and keep with only one object.

Like this:

def add1(my_list):
    my_list.append(1)

a = []
add1(a)
print a

But, if you do my_list = [1], you obtain a new instance, losing the original reference inside the function, that's why you can't just do "my_bool = False" and hope that outside of the function your variable get that False

Rafael SDM Sierra
+1  A: 

You can use a list to enclose the inout variable:

def func(container):
    container[0] = True


container = [False]
func(container)
print container[0]

The call-by-value/call-by-reference misnomer is an old debate. Python's semantics are more accurately described by CLU's call-by-sharing. See Fredrik Lundh's write up of this for more detail:

ars
if you are only using container for this purpose, there is no need to check `container[0]`. empty `container` for false and stick anything in it for true and then just test the container directly.
aaronasterling
Sure, but I don't know the OP's use case from the question. I'm just showing one possible scenario; hopefully, the general method is clear enough.
ars
+1  A: 

Python (always), like Java (mostly) passes arguments (and, in simple assignment, binds names) by object reference. There is no concept of "pass by value", neither does any concept of "reference to a variables" -- only reference to a value (some express this by saying that Python doesn't have "variables"... it has names, which get bound to values -- and that is all that can ever happen).

Mutable objects can have mutating methods (some of which look like operators or even assignment, e.g a.b = c actually means type(a).__setattr__(a, 'b', c), which calls a method which may likely be a mutating ones).

But simple assignment to a barename (and argument passing, which is exactly the same as simple assignment to a barename) never has anything at all to do with any mutating methods.

Quite independently of the types involved, simple barename assignment (and, identically, argument passing) only ever binds or rebinds the specific name on the left of the =, never affecting any other name nor any object in any way whatsoever. You're very mistaken if you believe that types have anything to do with the semantics of argument passing (or, identically, simple assignment to barenames).

Alex Martelli