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
  • Why do you want to? What's the context that prohibits you from simply returning a value? Commented Sep 6, 2010 at 1:26

3 Answers 3

8

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:

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
A similar, but perhaps dirtier, method is to have the user pass locals() or globals(), and edit that dict directly.
3

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.