Style qeustion: Multiple return values

dn PythonList at DancesWithMice.info
Tue Apr 13 02:24:43 EDT 2021


On 12/04/2021 22.32, Chris Angelico wrote:
> On Mon, Apr 12, 2021 at 8:20 PM dn via Python-list
> <python-list at python.org> wrote:
>>
>> On 12/04/2021 20.29, Steve Keller wrote:
>>> Just a short style question: When returning multiple return values, do
>>> you use parenthesis?
>>
>> Thus, the answer to your question is a matter of style, and thus the
>> understanding of those who might read the code.
>>
> 
> Yes, just like the subject line says :)


It's cheeky day at your place is it?

Time for your English lesson: with correct vocal-emphasis, the first
part of the statement reads as an affirmation or agreement with the OP:

    ...the answer to your question *is* a matter of style...

Perhaps I should have written in English-English prose?

    ...the answer to your question *is indeed* a matter of style...


To follow-through, the second clause offers an idea of how the OP might
like to proceed in settling-on his/her own style...


> My personal preference: If the purpose is simply to return two values
> (which will almost always be unpacked immediately), I would omit the
> parens.

+1

In response to A.N.Other who countered this view, my aged-eyes agree that

    a,b = foo()

ie without space, is asking for trouble. It's far too easily (mis-)read as:

    ab = foo()

which is why such single-character horizontal-spacing is as helpful to
us silver-surfers as Python's block indentation idiom! Thus:

    a, b = foo()

ie with space.

Artists and designers refer to such as "the value of negative space"!


To this end, in *my style* (ie the one, true, and only-acceptable way to
write Python#) spaces are used* to assist the eye to separate ("parse")
the components of the code-line - and conversely, a lack of spaces to
indicate a 'tight linkage' within looser lists/streams of elements.

For functions, adding a space after the opening and before the closing
parentheses helps identify the beginning and end of the list of
arguments or parameters, eg

    def my_function( fred, barney, wilma, betty ):

NB putting a space before the left-parenthesis creates a disconnection:

    def my_function ( fred ### or even my_function (fred, ...

To my taste (which may all be in my mouth), the spaces after each comma
(and "betty" may also be followed by an optional/notional comma) help to
make it clear that there are four parameters.

However, it may seem confusing to then offer:

    def my_function( fred=None, barney=None, etc ):

That said "fred=None" is a single unit, and clearly separated from
"barney", whereas:

    def my_function( fred = None, barney = None, etc ):

does not seem to help the eye associate the two?four parameters with
their default values - indeed to be able to reliably count that there
are two?four parameters within the function signature!


However... adding typing to the mix does give pause for thought.

Now, each parameter is a veritable list of object-name, colon, type
(which may itself be multi-part), equals, default-value. Phew!

The solution (again, to my taste) is to define each parameter on its
own/a separate line - using the parentheses to both group the
parameter-list and to continue the statement in multi-line form.


YMMV!

- also, your eyes may be better than mine!
(in which case) My eyes have probably seen a lot more than yours - and
if you'd seen some of the code-messes I've seen, your eyes would be
clouding-over too!


* even if some, including our linter over-lords, seem to feel they are
"wrong"!

# if you believe this, you'll believe anything...
-- 
Regards,
=dn


More information about the Python-list mailing list