determining the number of output arguments

Jp Calderone exarkun at
Sun Nov 14 23:21:52 CET 2004

On Sun, 14 Nov 2004 17:12:24 -0500, Darren Dale <dd55 at> wrote:
> 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.


More information about the Python-list mailing list