Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include overlength URLs in python comments

When creating python code and keeping to the PEP8 style guide, one can have trouble to limit the line length to 79 characters when quoting a long URL in a comment:

def foo():
    # see http://stackoverflow.com/questions/13062540/replacing-text-using-regular-expression-in-python-with-named-parameters
    do_something()

In the actual code this looks ugly, when the URL comment overlaps with the otherwise empty indentation area on the left of the code. Is there some way to handle this in a better way, while I am still able to easily copy-and-paste the URL to put it into a web-browser?

like image 818
Alex Avatar asked Oct 25 '12 06:10

Alex


People also ask

How do you comment Big codes in Python?

The most straight-forward way to comment out a block of code in Python is to use the # character. Any Python statement that begins with a hashtag will be treated as a comment by the compiler. There's no end to how many block comments you can have, in a row or otherwise.

How do you add a link to a Python comment?

hover on the link & get a pop-up with a link to press. press 'option'+'click' on the URL at the comment itself.

How do you put a URL in a comment?

So, here are the steps to insert a hyperlink into a comment on your LinkedIn post: Copy your web link. Create a comment below your post. Paste the link to the site.

How do I put links in comments in stackoverflow?

Basic Links You can also select text and press CTRL + L to make it a link, or press CTRL + L with no text selected to insert a link at the current position.


1 Answers

Of the many possible way, use a \ at the end of line after closing the quotes

url = "http://stackoverflow" \
      ".com"

response = urllib2.urlopen(url)
print response.read()

In case you have a field such as url split on multiple lines and want easier way to copy it back, use multi line string i.e. surround your whole url with 3 double quotes on both start and end. Then they can span any number of lines.

Edit: I wrote multiline comments which was suggested in comments and has been fixed

like image 50
fkl Avatar answered Oct 11 '22 05:10

fkl