I recently started working with Python/Django and I heard about the PEP 8 conventions. After having read PEP8 I had a better sense how to 'style' my code but I learned programming with Java and I used to just do whatever I like. Could you suggest how to put my example into PEP-8? much appreciated.
result = urllib.urlretrieve(
"https://secure.gravatar.com/avatar.php?"+
urllib.urlencode({
'gravatar_id': hashlib.md5(email).hexdigest(),
'size': srt(size)
})
)
Try downloading code style linters such as pep8 (a program which checks your code to see if it matches PEP 8 requirements) or pylint. You can find a more comprehensive list and comparison of Python style checkers here: What are the comprehensive lint checkers for Python?
In fact, there's a pep8 checker available online: http://pep8online.com/
If we run your code through that, it tells you:
Code Line Column Text
E126 2 29 continuation line over-indented for hanging indent
E225 2 70 missing whitespace around operator
E126 4 45 continuation line over-indented for hanging indent
E501 4 80 line too long (90 > 79 characters)
W292 7 30 no newline at end of file
The fixed version of your code would look more like this:
result = urllib.urlretrieve(
"https://secure.gravatar.com/avatar.php?" +
urllib.urlencode({
'gravatar_id': hashlib.md5(email).hexdigest(),
'size': srt(size)
})
)
In essence, the main PEP 8 violation you had was that you were indenting too much. A single indent is fine -- you don't need to align to the opening paren of the function calls. Python is also insistent that your lines don't go beyond 80 characters, but fixing the over-indentation also fixed that issue.