-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Added idioms to style.rst #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -7,6 +7,29 @@ Idioms | ||
|
||
Idiomatic Python code is often referred to as being *pythonic*. | ||
|
||
A common idiom for creating strings is to use `join <http://docs.python.org/library/string.html#string.join>`_ on an empty string.:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably easier to set up intersphinx with CPython and then just link in using that: http://sphinx.pocoo.org/latest/ext/intersphinx.html
Thanks for the feedback. I agree that these examples may not fit well in the idioms section. I wasn't exactly sure what the intent of the section was so I just tried those to see if @kennethreitz could give any guidance about what he expected. I like the idea of having a recipe section to isolate best-practices given common problems. |
When I think of idiomatic Python, I think of things that make sense in other languages which look "odd" in Python. A naive port of C (or PHP or Java or ...) code for a for loop might look like this:
whereas idiomatic/Pythonic code would be simply:
Taking it a step further, suppose we also need access to the list indices:
There are lots of other examples, I'm sure. A few others off the top of my head: list/tuple unpacking, and variable swaps (i.e. |
I think this would work quite well in a performance section. |
tbqh, I'm not sure where to go from here. Should I close the pull request and wait for for you to make a performance section? I understand where you're coming from as far as moving this to a performance section. I'm new to making pull requests and how the direction of a project like this should progress. I'd like to know what your thoughts on moving forward are. |
I'd suggest:
|
This is fantastic stuff. Sorry for the delay! |
Added idioms to style.rst
Not sure what the section on idioms was supposed to contain.
The first idiom using str.join() I've seen used a lot and has been recommended in several places as being better than using a for loop to concatenate strings to an already existing string.
The 2nd idiom is to use dictionaries when you need to perform lookups. I consider it idiomatic because it's not obvious that you should exploit the hashtable lookup this way. In this example I mapped from a string to an empty list but mapping to any arbitrary thing (None, False, '', etc.) would work equally as well.
It seems to me that the dict lookup should go under the idioms section rather than performance.