nargout in Python

前端 未结 5 1126
甜味超标
甜味超标 2020-12-06 16:21

Does Python have any equivalent of nargout in MATLAB? I find nargout a very neat approach if we want to keep the number of return parameters flexible. Is there a way I can f

5条回答
  •  日久生厌
    2020-12-06 16:40

    In Matlab, the number of output arguments is actually one of the inputs to a function. This is not the case in Python and so you have set up the function's interface to reflect this differently.

    For example, here's a function that applies upper() to a group of strings, and the user can expect the the number of inputs equals the number of outputs. The syntax is also very similar to Matlab's.

    >>> def var_returns_f(*args):
    ...     return [str.upper() for str in args]
    ... 
    >>> 
    >>> a, b = var_returns_f('one', 'two')
    >>> 
    >>> a
    'ONE'
    >>> b
    'TWO'
    

提交回复
热议问题