tags:

views:

67

answers:

3
+1  Q: 

Python out params?

Is it possible to do something like this:

def foo(bar, success)
    success = True
    # ...

>>> success = False
>>> foo(bar1, success)
>>> success
True

Does Python have out params, or an easy way to simulate them? (Aside from messing with parent's stack frames.)

+6  A: 

You have multiple return values.

def foo(bar)
    return 1, 2, True

x, y, success = foo(bar1)
zneak
A: 

Yes, put them in a dict as pass the dict as a parameter. I think it's somewhere in the main python official tutorial.

A: 

In python you can not update an immutable type from inside a function (like the boolean in your example or an int or a string or a tuple, ...). Period. So your options are as illustrated in previous answers:

  1. Wrap the immutable type in a mutable type (like a list, array or object).
  2. Use multiple return values