13

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?

Johannes P.
4,1634 gold badges28 silver badges27 bronze badges
asked Aug 23, 2011 at 8:25
3
  • 5
    Break it up, and add error handling. For example, what happen if there's no network connection. Commented Aug 23, 2011 at 8:28
  • 2
    IMHO, the intent is clearly expressed by the one-liner. I don't see the need to split it up into multiple lines. In fact, in this case, I might go even further and do getlink = lambda url: urllib2.urlopen(url).readlines()[425].split('"'). This of course assumes that no exceptions are generated; if that is a case you want to handle within the function, the broken-up form is better, since you can add appropriate try catch finally blocks. Commented Aug 23, 2011 at 8:28
  • 3
    @Dikei At the moment, if there is no network connection urllib spits out a socket error. I could add a nicer error message, but that's all it would be; it would exit at the same point. Commented Aug 23, 2011 at 8:33

6 Answers 6

19

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.

answered Aug 23, 2011 at 8:50
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of just reiterating what the code indexing does, why not insert an example of what content[425] might contain, to show what the 8th section has in it?
Nevermind that this looks like one of the most fragile web-scraping scripts I've ever seen!
@Paul I can't agree more about this not being the way to scrape pages. Regarding the inclusion of a snippet of the target line as comments, well it's an improvement but not without its shortcomings - we end up with two sections to edit when the data changes. Change one and not the other, and you end up with a spoonful of confusion.
"... as long as it fits in one eye-full..." That is why PEP 8 still recommends limiting line lengths to 79 characters, even in a post-80-column-display world. Therefore the one-liner is still acceptable (and still reads quite easily IMHO), although debugging will likely be harder.
3

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]
answered Aug 23, 2011 at 11:11

Comments

2

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.

answered Aug 23, 2011 at 8:44

Comments

2

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.

answered Aug 23, 2011 at 9:17

2 Comments

To me it's exactly the opposite. I draw more useful information from reading calls to standard library functions than from an unknown programmer's naming style, however well thought out it might be.
@Nicola Musatti Exactly the same for me.
1

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 .

answered Aug 23, 2011 at 8:29

Comments

1

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! :)

answered Aug 23, 2011 at 8:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.