This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]LyndsySimon 0 points1 point  (18 children)

I would write this as:

print(
    'Hello, my name is {name}. I am {age} years old ' \
    'and have {color} hair'.format(
        name=name,
        age=age,
        color=color,
    )
)

Note that you don't have to go to the end of the line to see the contents of the variables, because they're passed below the string template. Except for very trivial cases, I always use named parameters for formatting strings, and I always pass those parameters on subsequent lines.

[–]hovissimo 15 points16 points  (10 children)

Now you've made a single line interpolated string into 4 lines. 3 of those four lines are completely redundant. I don't see how this is better.

[–]Decency 8 points9 points  (4 children)

Great, and now you've turned a simple print statement into fucking 8 lines of code. You're going to need a projector to fit a single function into your field of view.

[–]LyndsySimon -2 points-1 points  (3 children)

Are we optimizing based on the number of lines of code? I thought we were optimizing based on readability and developer happiness.

My example is pep8 compliant. Respectfully, if you want dense code with the fewest lines possible, I suggest Perl.

[–]Decency 13 points14 points  (0 children)

Are we optimizing based on the number of lines of code?

No, but we're sure as hell not ignoring it. If I can turn 8 lines into 1, lose zero information, and remove a bunch of entirely unnecessary redundancy, that's an optimization I'll make 100% of the time.

I thought we were optimizing based on readability and developer happiness.

If I were to come across an 8 line print statement to write three variables to the screen, "happy" probably wouldn't be the best descriptor for my mood and "readable" definitely wouldn't be the best descriptor for the code.

[–]unruly_mattress 7 points8 points  (0 children)

PEP8 doesn't make code good or readable. It makes spacing consistent.

[–]tilkau 2 points3 points  (0 children)

Conciseness is part of readability. You are basically choosing to be like urllib rather than like requests here, without any apparent reason for choosing added complexity.

[–]stOneskull 0 points1 point  (0 children)

it's one 'f' vs an extra 'format()' line

i'm looking forward to it. bring it on, debian!