# Style guide on writing long Python code lines [closed]

When building a working example of a class, I ended up with the following statements which is violating the 80 character rule of PEP8. The code was inside a method inside a class.

Code excerpt is from PrettyTable.__init__() and PrettyTable.add_column() in my answer to Using changeable column formatting to display table. Have however left out some of the surrounding code to make it easier to get better reviews:

class SomeClass():
def some_method(self):
if self._replace_space_char == None:
self._replaced_column_separator = self._column_separator
else:
self._replaced_column_separator = self._column_separator.replace(' ', self._replace_space_char)



Do you have any suggestion as to how to make this readable, and still maintain good variable names and legal Python code? I'm especially interested in how to fold/wrap/... when the construct self._variable_name.some_method() is by it self in excess of 30-50 characters.

You should be able to get here solely from reading PEP8. Also, don't use == to compare with singletons like None, use is instead:

class SomeClass():
def some_method(self):
if self._replace_space_char is None:
self._replaced_column_separator = self._column_separator
else:
self._replaced_column_separator = self._column_separator.replace(
' ', self._replace_space_char,
)

self._replaced_column_separator,
)
)


From PEP8:

The 4-space rule is optional for continuation lines.

Optional:

# Hanging indents *may* be indented to other than 4 spaces.
foo = long_function_name(
var_one, var_two,
var_three, var_four)


This allows obviously nested arguments to be differentiated from code blocks.

Not in PEP8, I like to end arguments with a comma so that insertion of new arguments in version control only affect a single line. Imagine the following was in a diff from version control, and note the single additional line, and no removed lines:

          self.HEADER_PART.format(

• Will the extra comma affect number of elements if using something like my_func(*args)? Or wil it be stripped away before the function is called? – holroy Sep 30 '15 at 22:15