Improvement to imports, what is a better way ?

Thomas Passin list1 at tompassin.net
Thu Jan 19 14:09:54 EST 2023


On 1/19/2023 1:30 PM, 2QdxY4RzWzUUiLuE at potatochowder.com wrote:
> On 2023-01-19 at 12:59:21 -0500,
> Thomas Passin <list1 at tompassin.net> wrote:
> 
>> Well, it's an art, not a science [...]
> 
> +1
> 
>> # Create a plot
>> g2 = (
>>        ggplot(df2,
>>        aes('Days Since Jan 22',  # Comments can clarify these params
>>        + geom_point(size=.1, color='blue') # size, color params optional
>>        + theme_bw() # Optional theme (background, grid color, ...)
>>       )
> 
> You've got a comma followed by a plus sign in there, so I'm not exactly
> sure where the parameters to which function begin and end.
> 
> When it starts to look like this, I begin breaking out the parameters:
> 
> label = 'Days Since Jan 22'
> size = geom_point(size=.1, color='blue')
> theme = theme_bw()
> g2 = ggplot(df2, aes(label, size, theme))
> 
>> # Compose a long string:
>> msg = ('A very long line .....\n'
>> 	+ 'Another long bit of text ....'
>> 	+ 'plus another ....'
>>        )
> 
> If all the pieces are constants, then Python will concatenate them for
> you:
> 
> msg = ('A very long line .....\n'
>         'Another long bit of text ....'
>         'plus another')

Yes, the "+" sign is not actually needed in Python, but I find it's 
helpful anyway. Using one also mirrors the use of other operators, such 
as "or", where you cannot omit the operator.  Another matter of taste ...

> You can even mix in "f" strings:
> 
> msg = ('long line\n'
>         f'left text {name} right text'
>         'more here')
> 
> But watch out for missing spaces between the pieces!  :-)
> 
>> The PEP-8 rules are good, but they can't cover all cases perfectly.
> 
> Some the PEP-8 rules are debatable.  Regardless, they can't cover all
> cases perfectly.  (IOW, we agree on the bit that's relevant to this
> thread.)



More information about the Python-list mailing list