Strigoides
Strigoides

Reputation: 4509

Best python style for complex one-liners

I recently wrote a rather ugly looking one-liner, and was wondering if it is better python style to break it up into multiple lines, or leave it as a commented one-liner. I looked in PEP 8, but it did not mention anything about this

This is the code I wrote:

def getlink(url):
    return(urllib.urlopen(url).readlines()[425].split('"')[7])
    # Fetch the page at "url", read the 426th line, split it along
    # quotes, and return the 8th quote delimited section

But would something like this be better style?:

 def getlink(url):
    url_file = urllib.urlopen(url)
    url_data = url_file.readlines()
    line = url_data[425]
    line = line.split('"')
    return line[7]

Or perhaps something in between?

Upvotes: 13

Views: 1808

Answers (6)

eyquem
eyquem

Reputation: 27585

I also find the expression urllib.urlopen(url).readlines()[425].split('"')[7] rather comprehensible.

However, I would prefer:

def getlink(url):
    line425 = urllib.urlopen(url).readlines()[425]
    return line425.split('"')[7]

Upvotes: 3

Wok
Wok

Reputation: 5323

The multi-line version conveys semantics, which the one-liner makes harder to grasp.

This is how I read it:

 def getlink(url):
    url_file = ...
    url_data = ...
    line = url_data[425]
    ... = ... .split('"')
    return line[7]

Which means I can get the important parts faster and easier, without scrumbling through a long expression mixing:

  • general calls to urlopen() and readlines() (obvious for a function called getlink(url))
  • and more specific parts (url_data[425] and line[7]).

However, Shawn Chin's version is even easier to read.

Upvotes: 2

Shawn Chin
Shawn Chin

Reputation: 86924

My vote would be based on readability. I find your one-liner quicker to digest than the multi-line example.

One-liners are great as long as it fits in one eye-ful, and collectively they perform one distinct task.

Personally, I would write that as:

def getlink(url):
    content = urllib.urlopen(url).readlines()  
    return content[425].split('"')[7]  

(Now, venturing into downvote realm...)

Your block of comments is great for someone unfamiliar with Python, but arguably, they reduce readability by increasing the information to digest. A pythonista reading the code would quickly understand your one-liner, and yet may then proceed to read the comments just in case there are caveats or edge cases to be warned of.

I'm not saying comments are evil, just that verbose comments can have a negative effect on readability. E.g. the classic : x+=1 # increment x by 1

Naturally, this is down to the purpose and audience of the code.

Upvotes: 19

maulik13
maulik13

Reputation: 3760

To me multi-line version is much better. With multi-line code you break up the logic and use variables to store intermediate output. The variable names then allow me to read the logic and see what my output depends on. Also you don't have to write elaborate comments in this case. I find it easier to read the multi-line version after some months than read the single line version in such cases. The example you posted is not complex, but just to keep consistency I would have written your example code in multiple lines.

Upvotes: 2

Ethan Furman
Ethan Furman

Reputation: 69160

The multiline version is better Python style. It is easier to read, easier to understand, and easier to modify.

This is Python -- easy is good! :)

Upvotes: 1

Andrei Ciobanu
Andrei Ciobanu

Reputation: 12848

Your one-liner is not that obscene (at least for my eyes), plus it's a good thing you've added the comments .

When write software, think of yourself in 8 months or so, looking again at this piece of code. It should be as readable then, as you perceive it today .

Upvotes: 1

Related Questions