开发者

Python out params?

开发者 https://www.devze.com 2023-01-03 23:04 出处:网络
Is it possible to do something like this: def foo(bar, success) success = True # ... >>> success = False

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:

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

精彩评论

暂无评论...
验证码 换一张
取 消