determining the number of output arguments

Darren Dale dd55 at cornell.edu
Sun Nov 14 21:52:01 EST 2004


Jp Calderone wrote:

> On Sun, 14 Nov 2004 17:12:24 -0500, Darren Dale <dd55 at cornell.edu> wrote:
>>Hello,
>> 
>> def test(data):
>> 
>> i = ? This is the line I have trouble with
>> 
>> if i==1: return data
>> else: return data[:i]
>> 
>> a,b,c,d = test([1,2,3,4])
>> 
>> How can I set i based on the number of output arguments defined in
>> (a,b,c,d)?
>> 
> 
>   There is a recipe in the ASPN cookbook for this.  I wonder, though.  Why
>   do you see this as preferable to the more obvious form of:
> 
>     def test(data):
>         # ...
>         return data
> 
>     a, b, c, d = test([1, 2, 3, 4])[:4]
> 
>   The added "magic" does little but make it easier to write buggy code and
>   harder to understand the behavior of the "test" function.
> 
>   Jp


I want to extend the capabilities of an existing function without breaking
backward compatibility. I used nargin and nargout (number of arguments in
and out) pretty extensively in Matlab.




More information about the Python-list mailing list