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 eas开发者_如何转开发y way to simulate them? (Aside from messing with parent's stack frames.)
You have multiple return values.
def foo(bar)
return 1, 2, True
x, y, success = foo(bar1)
Yes, put them in a dict as pass the dict as a parameter. I think it's somewhere in the main python official tutorial.
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:
- Wrap the immutable type in a mutable type (like a list, array or object).
- Use multiple return values
精彩评论