'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?
Solution 1:[1]
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
Solution 2:[2]
I'm using VS Code and you have 2 options:
- hover on the link & get a pop-up with a link to press.
- press 'option'+'click' on the URL at the comment itself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Elad Rubi |

