Friday Finking: initialising values and implied tuples

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Fri Apr 2 18:58:55 EDT 2021


On 2021-04-02 at 19:25:07 -0300,
Marco Ippolito <maroloccio at gmail.com> wrote:

> > (a) basic linear presentation:
> > 
> > resource = "Oil"
> > time = 1
> > crude = 2
> > residue = 3
> > my_list = "long"
> > 
> > (b) using explicit tuples:
> > 
> > ( resource, time, crude, residue, my_list ) = ( "Oil", 1, 2, 3, "long" )
> > 
> > (c) linear and indented tuples:
> > 
> > (
> >     resource,
> >     time,
> >     crude,
> >     residue,
> >     my_list
> > ) = (
> >     "Oil",
> >     1,
> >     2,
> >     3,
> >     "long"
> > )
> 
> Choose: (a).

I agree.

That said, I sometimes end up with something like this:

    ( resource, time, crude, residue, my_list ) = \
        ( "Oil", 1, 2, 3, "long" )

which mitigates matching up which value goes with which name.  But as
soon as either tuple overflows one physical line, it's back to (a).

I did go through a phase where I tried (c); note the past tense.

> In (b) I have a problem matching identifiers to values horizontally at
> a glance and in (c) I have the same problem vertically: i.e. "is 3 the
> value for residue or crude above/to-the-left?"
> 
> Cognitive burden slows down and fatigues.

Yep.

> Alternatively, if the data "fits together", use a `namedtuple` with
> kwarg initialisation or structured data types like `dataclasses`.

IMO, that's usually more troule than it's worth, although

    thing = new_thing(
                resource="Oil",
                time=1,
                crude=2,
                residue=3,
                my_list="long")

is often better than a collection of positional parameters.


More information about the Python-list mailing list