How Do I Keep Python Code Under 80 Chars Without Making It Ugly?

Solution 1:

Your code style seems to insist that if you break a line inside a parenthesis, lines below need to line up with it:

self.SomeLongLongName = SomeLongLongName.SomeLongLongName(some_obj,
                                                          self.user1
                                                          self.user2)

If you are willing to drop this requirement, you can format the code as follows, where continued lines have a fixed double indent:

self.SomeLongLongName = SomeLongLongName.SomeLongLongName(
        some_obj, self.user1, self.user2)

This avoids writing code down the right-hand margin on the page, and is very readable once you are used to it. It also has the benefit that if you modify the name of "SomeLongLongName", you don't have to re-indent all of the following lines. A longer example would be as follows:

if SomeLongLongName.SomeLongLongName(
        some_obj, self.user1, self.user2):
    foo()
else:     
    bar()

The double indent for continued lines allows you to visually separate them from lines indented because they are in an if or else block.

As others have noted, using shorted names also helps, but this isn't always possible (such as when using an external API).

Solution 2:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

PEP 8 Style Guide for Python Code (follow link for examples).