Ayrx
Ayrx

Reputation: 2162

Python - How to style this line of code?

First of, I'm not sure if SO is the right place for this question, so feel free to move it somewhere more appropriate if necessary.

cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))

I have this line of code. The Python PEP 8 document recommends limiting lines to 79 characters to preserve readability on smaller screens.

What is the most elegant way to style this line of code to fit the PEP recommendations?

cmd_folder = os.path.realpath(os.path.abspath(os.path.split
                               (inspect.getfile
                                 ( inspect.currentframe() ))[0]))

Is this the most appropriate way for is there a better one that I have not thought of?

Upvotes: 0

Views: 257

Answers (4)

jdi
jdi

Reputation: 92559

I would say in the case of your example code, it would be more appropriate to split them up into individual operation, as opposed to making the code harder to read.

aFile = inspect.getfile(inspect.currentframe())
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(aFile)[0]))

It should not be such a chore to trace the start and close of all those parenthesis to figure out what is happening with each temp variable. Variable names can help with clarity, by naming the intention/type of the results.

If it is two, maybe 3, nested calls I might do the act of newlines in one call, but definitely not when you have a ton of parenthesis, and list indexing squished in between. But normally I am only more inclined to do that with chained calls like foo.bar().biz().baz() since it flows left to right.

Always assume some poor random developer will have to read your code tomorrow.

Upvotes: 5

Karol Nowak
Karol Nowak

Reputation: 662

If you really insist on keeping it one expression, I'd go with an "extreme" simply because it actually looks nice:

cmd_folder = os.path.realpath(
    os.path.abspath(
        os.path.split(
            inspect.getfile(
                inspect.currentframe()
        ))[0]
))

Upvotes: 0

Tadeck
Tadeck

Reputation: 137300

I am in general with the answer provided by jdi (split into several expressions). But I would like to show another aspect of this issue.

In general, if just trying to properly break and indent this line, you should also follow PEP8 rules on indentation:

Use 4 spaces per indentation level.

For really old code that you don't want to mess up, you can continue to use 8-space tabs.

Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

[several examples follow]

So in your case it could look like this:

#---------------------------------------------------------79th-column-mark--->|
cmd_folder = os.path.realpath(
    os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]))

But as I mentioned at the beginning, and as PEP20 (The Zen of Python) mentions:

Flat is better than nested.

Sparse is better than dense.

Readability counts.

thus you should definitely split your code into few expressions, as jdi notes.

Upvotes: 4

wberry
wberry

Reputation: 19327

I tend to do it like this:

cmd_folder = os.path.realpath(os.path.abspath(os.path.split(
    inspect.getfile(inspect.currentframe()))[0]))

Open-parens at the end of the first line, and indention on continuation lines. Ultimately it boils down to what you think is more aesthetic.

There's also no shame in doing part of the computation and saving the result in a variable, like this:

f = inspect.getfile(inspect.currentframe())
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(f)[0]))

Upvotes: 1

Related Questions