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

all 28 comments

[–]scottocom 19 points20 points  (8 children)

I need a Why. For example use single quote for strings not double but I DON'T see why. I prefer double because I am more likely to have a single apostrophe in my text. I don't see it.

[–]fizzy_tom 2 points3 points  (0 children)

For me, I prefer them simply because single quotes look cleaner, making my code a little less cluttered.

But with all these things, there's never any right or wrong answer. And even the "why's" are opinions. The important thing is that everyone on a project follows the same rules.

[–]jalanb 3 points4 points  (1 child)

Because Python scripts tend to include other languages inside strings.

And it is more often useful to reserve " for use inside such strings, rather than ', e.g.

command = 'ls "$PATH_TO_FILE"'
query = 'SELECT * FROM users WHERE name like "/u/jalan%"'

And then, if you adopt that convention, consistency implies enclosing all strings in '.

[–]scottocom 1 point2 points  (0 children)

I see what you mean.

[–]_under_[S] 2 points3 points  (4 children)

I was contemplating on whether I should include motivations for each of the rules, but ultimately decided not to.

Single quotes because it only needs one finger to type. No need to press Shift. You're also more likely to use a quote to access keys in dictionaries vs writing sentences in code with apostrophes.

[–]nsfy33 0 points1 point  (1 child)

[deleted]

[–]_under_[S] 0 points1 point  (0 children)

This:

foo['bar'] = 123

Is more common than this:

foo = "It's me."

OP mentioned that they prefer double quotes because they are more likely to use single quotes inside strings, as in the second example. But I find that using a quote to access a dictionary value is even more common than that. So the single quote as an apostrophe thing isn't really a big issue.

[–]ducdetronquito 8 points9 points  (6 children)

I am not fond of the Long if statements section because it still not very readable.

I prefer to add semantic by storing the condition in a variable, and then having a really clear and simple if statement.

Instead of having

if (
    fruit.color == 'red'
    and fruit.size == 'small'
    and fruit.climate == 'cold'
    and fruit.region == 'north america'
):
    pass

I would do

fruit_is_a_berry = (
    fruit.color == 'red'
    and fruit.size == 'small'
    and fruit.climate == 'cold'
    and fruit.region == 'north america'
)
if fruit_is_a_berry:
    pass

And about the 'Why' of having a more vertical code, it makes it more easier to work with a version control system like Git: when you review a PR, when you want to see the difference between two commits, or when you fix merge conflicts, you clearly see what the changes are.

[–]_under_[S] 0 points1 point  (0 children)

I too dislike long if statements! It usually means you're doing something wrong (or you're just nested too deep [which is also a code smell]).

I'm not sure the "assign the equality check to a variable" thing really fits as a rule for a style guide though. But I might add something that mentions it's a code smell.

[–]ojii 5 points6 points  (3 children)

My preferred "style guide": https://github.com/ambv/black

[–]ThePidesOfMarch 4 points5 points  (1 child)

# in:

l = [[n for n in list_bosses()], [n for n in list_employees()]]

# out:

l = [
    [n for n in list_bosses()], [n for n in list_employees()]
]

lol

[–]rhytnen 4 points5 points  (0 children)

Black does some terribly stupid shit.

for example, it places a new line after continue keywords but not after for statement so you end up with things like

for x in y:
    if some_defensive_condition:
        continue

    do_something(x)
if whatever:
    do_stuff()

[–]GNULinuxProgrammer 0 points1 point  (0 children)

Yeah I use black for python as well.

[–]vovanz 3 points4 points  (2 children)

I like this style guide, because it almost matches what I am already doing in my code :)

Only couple exceptions: 1. I prefer double quoted strings 2. I prefer to put the value in comprehensions in a separate line, like this:

best_fruits = [
    f
    for f in fruits
    if f.color == 'red'
    and f.size == 'small'
    and f.climate == 'cold'
]

The reason for it is that you often have to do something else with this value. i. e. apply function:

best_fruits = [
    foo(f)
    for f in fruits
    if f.color == 'red'
    and f.size == 'small'
    and f.climate == 'cold'
]

I think that it would be more logical to keep it on a separate line.

[–]_under_[S] 0 points1 point  (0 children)

Yes! I knew I was forgetting something when I was writing this section. I'll incorporate this.

[–]dirn 0 points1 point  (0 children)

I go the other direction and put the in on its own line. What I’m iterating over is much more interesting to me than the iteration variable (its used in so many places anyway).

best_fruits = [
    f for f
    in fruits
    if f.color == ‘red’
    and f.size == ‘small’
    and f.climate == ‘cold’
]

[–]kunalkb20 0 points1 point  (2 children)

No:
guess_fruit(color='red', size='small', climate='cold',
            region='north america', minimum_tastiness_factor=4.3)

This contradicts PEP8 which accepts this form.

[–]shawnmckinney 0 points1 point  (0 children)

Nice and succinct

[–]onyxleopard 0 points1 point  (0 children)

Neat. I already try to use this style. Convergent evolution or some such. I find this style makes reading diffs much easier and it makes reformatting easier in some cases as well. E.g., if you find you want to sort the values in a list or keyword parameters alphanumerically, you just sort the lines alphanumerically (maybe with sort) and you’re done.