Style qeustion: Multiple return values

Rob Cliffe rob.cliffe at btinternet.com
Mon Apr 12 14:13:29 EDT 2021


On 12/04/2021 09:29, Steve Keller wrote:
> Just a short style question: When returning multiple return values, do
> you use parenthesis?
>
> E.g. would you write
>
>      def foo():
>          return 1, 2
>
>      a, b = foo()
>
> or do you prefer
>
>      def foo():
>          return (1, 2)
>
>      (a, b) = foo()
>
>
> Steve
My personal view: It's a matter of style; do whatever looks best to 
you.  Looking at my own code I'm not consistent in my choice.  That said:
     return item, cost    # If I think of these as two distinct values 
I'd be inclined to omit the parentheses.
     return (x,y)              # If I'm returning a point which I think 
of as a single entity, I'd probably put them in,
                                       #   especially, for consistency, 
if the rest of my code contains lots of `(x,y)`s, `(u,v)`s etc.
     return (long-expression1, long-expression2)
                                        # It might be hard to spot the 
comma at first glance so the parentheses might help to recognise a tuple.
Best wishes
Rob Cliffe


More information about the Python-list mailing list