stylistic question -- optional return value

John La Rooy igetenoughspamalreadythanksjlr at doctor.com
Thu Aug 29 07:52:05 EDT 2002


> 
> Can you imagine them becoming confused by 1-lists?  Why not just
> return a list, always, with one or more items in it.  len() is
> available to check which you have if you need it.
> 
> 
>>What is the most Pythonic way of solving this problem?
> 
> 
> Personally, I'd probably use a list because I eschew tuples most
> of the time.  In this case, if I thought I wanted a tuple, I would
> return a tuple and assume no one would get confused by them.
> 
> The only time I imagine someone getting confused by a 1-tuple
> is when someone is expected to create one to pass *in* to my
> functions, not when my function is returning one.
> 
> Maybe I'm wrong about that, but I've never heard of anyone being
> confused upon receiving a 1-tuple.
> 
> Definitely, in my opinion, all the other suggestions including
> sentinels and special classes, are much less Pythonic than a
> nice simple list or tuple.
> 
> -Peter

struct.unpack returns a variable length tuple which can be handy

point.x,point.y=struct.unpack("...

or

point.x,=struct.unpack("...
point.y,=struct.unpack("...

Either way we are only returning *one* value - a tuple in this case

Consider: Should struct.unpack special case when there is a one tuple
so we can write

point.x=struct.unpack("...
point.y=struct.unpack("...

I don't think so.


<gripe> Why does struct.pack use a variable number of arguments instead 
of a format string and a tuple? </gripe>

John




More information about the Python-list mailing list