1

Goal: make the following function PEP8 compliant. The second line is too long (>79 chars) and needs to be split up.

def a_function():
    gdf_out = gdf_out.set_index(['level_0', 'level_1']).set_geometry('geometry')

This is a chained method. Following another answer results in:

def a_function():
    gdf_out = (
            gdf_out.set_index(['level_0', 'level_1'])
            .set_geometry('geometry')
        )

However this seems a bit odd to me, especially the indentation. Is there a better practice for chained commands in python PEP8?

What about this?

def a_function():
    gdf_out = (gdf_out.set_index(['level_0', 'level_1'])
                      .set_geometry('geometry'))
Rutger Hofste
  • 4,073
  • 3
  • 33
  • 44
  • 3
    Or how about `levels = ['level_0', 'level_1']` then `gdf_out = gdf_out.set_index(levels).set_geometry('geometry')` ? – khelwood Feb 19 '18 at 09:37
  • 1
    good suggestion that will work in this case. How about longer chained commands for which this is not enough? – Rutger Hofste Feb 19 '18 at 09:40
  • 1
    Your last example with the `.set` left-aligned looks more readable (to me) and it's akin to [binary operator alignment](https://www.python.org/dev/peps/pep-0008/#id20). Also bear in mind [this](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds). – RolfBly Feb 19 '18 at 09:43
  • @khelwood a temporary variable just to stop multi-line code? Does not seem necessary to me. I like the 1st one, but I could move the final parentheses to the line above – Chris_Rands Feb 19 '18 at 09:47
  • Or, `gdf_out = gdf_out.set_index(['level_0', 'level_1'])`, newline then, `gdf_out = gdf_out.set_geometry('geometry')` – Arount Feb 19 '18 at 09:50
  • @Arount thanks but that would not make full use of the benefits of chaining, wouldn't it? – Rutger Hofste Feb 19 '18 at 09:54
  • In term of cosmetic yes, in functional / technical terms, not.. – Arount Feb 19 '18 at 09:56
  • 1
    @RutgerHofste or https://gist.github.com/arount/18fe9c97f6a9a8ac257ff90b0486dad6 ? – Arount Feb 19 '18 at 09:59
  • also a good option. However the chaining commands do not appear to have the same hierarchy. In the example above it is very easy to see that two methods have been applied i.e. set_index() and set_geometry() – Rutger Hofste Feb 19 '18 at 10:03
  • 3
    Remember PEP8 is a style *guide* not a strict set of rules. That line is spot on 80 chars, and arguably is clearest left as a single line, not artificially split to meet PEP8. I'd recommend watching Raymond Hettinger's talk [Beyond PEP 8](https://www.youtube.com/watch?v=wf-BqAjZb8M) for some background as to why PEP8 shouldn't always be followed. – match Feb 19 '18 at 10:20

1 Answers1

2

This answer does provide another alternative that complies with PEP8 Standard.

def a_function():
    gdf_out = gdf_out.set_index(['level_0', 'level_1']) \
                     .set_geometry('geometry')

\ at the end of line is continuation character.

Aditya Shaw
  • 323
  • 4
  • 11