Question

What is the preferred style for assigning values to variables when the variables are nested several levels deep, have fairly long names, and are being assigned fairly long values/expressions.

For example:

if this:
    if that:
        if here:
            if there:
                 big_variable['big_key']['big_value'] = another_big_variable_that_pushes_line_over_79_characters
                 other_thing = something

The character-limit breaches are only in the single digits, but I want to clean up my code so that it follows PEP 8 as faithfully as possible. I've done the following, but I am still fairly new to python, and I'm not sure if it's the sort of thing that would make an experienced python programmer cringe:

if this:
    if that:
        if here:
            if there:
                big_variable['big_key']['big_value'] = \
                    another_big_variable_that_pushes_line_over_79_characters
                other_thing = something

I get the impression that the line continuation character is somewhat taboo; but I can't really think of a cleaner solution than the one I have if I'm working with these big dictionaries and I can't make a clean break in the middle of the variable name.

Was it helpful?

Solution 2

Line continuation is a bit taboo, but not the end of the world. We must always strive to write code such that some other programmer down the line can understand what we were doing.

Using the line continuation character \ is but one tool in our arsenal for achieving this goal of legibility.

Naming conventions are another issue. As da Vinci said "Simplicity is the ultimate sophistication." If you can make variable names small AND understandable, then you are sophisticated ;-). It's too easy to just say var1, var2, var3 etc. Coming up with good names is a skill, which takes effort.

Would you rather see a variable named ChiefExecutiveOfficerOfCompanysName or CEOName?

If you can combine if statements, then your code can become even more legible. Chances are, if you have some big hierarchy of if...else-if, then you're doing something wrong (this is a code smell). For example, you might change this:

if this:
    if that:
        if here:
            if there:

Into this:

if this and that and here and there:

Or toss such gross logic into an evaluator function like so:

if EvaluateConditions(<args>):

Breaking code up into logical pieces, and putting those pieces into functions is another way to make things readable (we only have so much RAM, and we'd like to fit entire functions in it... humans aren't very good at paging)

Avoid copying and pasting code with slight changes by using parameterized functions, or some good design patterns

OTHER TIPS

I don't think there is any problem with line continuation in Python. But sometimes I prefer this:

big_variable['big_key']['big_value'] =(
    another_big_variable_that_pushes_line_over_79_characters
)

It is useful in long expressions, too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top