开发者

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:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜