-4
\$\begingroup\$

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)

        self._header_text.append(self.HEADER_PART.format(title, header_alignment, width, self._replaced_column_separator))

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.

\$\endgroup\$
2
\$\begingroup\$

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._header_text.append(
          self.HEADER_PART.format(
            title, header_alignment, width, 
            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(
            title, header_alignment, width, 
            self._replaced_column_separator,
+           new_arg,
          )

Python ignores the ending comma.

More thoughts when looking at this code: To improve readability, consider changing to keyword arguments, especially if you have sensible defaults. You can go to one line per argument as well.

\$\endgroup\$
  • \$\begingroup\$ It looses a little on readability, but then again that is the major part of this question how not to loose readability... \$\endgroup\$ – holroy Sep 30 '15 at 22:04
  • \$\begingroup\$ Just to note you are allowed 'Aligned with opening delimiter' indents, as well. \$\endgroup\$ – Peilonrayz Sep 30 '15 at 22:12
  • \$\begingroup\$ 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? \$\endgroup\$ – holroy Sep 30 '15 at 22:15
  • \$\begingroup\$ Python ignores the ending comma. \$\endgroup\$ – Aaron Hall Sep 30 '15 at 22:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.