-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.:: | ||
|
|
||
| letters = ['s', 'p', 'a', 'm'] | ||
| word = ''.join(letters) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably isn't the best example. This pattern is idiomatic (and better-performing) than ProblemYou receive bits of data from one or more sources, but need to concatenate this into a single string for a return value SolutionUse the (There are probably better examples, this is just off the top of my head) |
||
|
|
||
| This will set the value of the variable *word* to 'spam'. This idiom can be applied to lists and tuples. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And generators and anything iterable that gives strings, like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't work, be because that's just a silly way of writing: with open("filename.txt") as f: which is a silly version of: chars = f.read()
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely a bad example. But there are better ones, I'm sure. Point is, not just lists or tuples are join-able. |
||
|
|
||
| Sometimes we need to search through a collection of things. Let's look at two options: lists and dictionaries. | ||
|
|
||
| Take the following code for example:: | ||
|
|
||
| d = {'s': [], 'p': [], 'a': [], 'm': []} | ||
| l = ['s', 'p', 'a', 'm'] | ||
|
|
||
| def lookup_dict(d): | ||
| return 's' in d | ||
|
|
||
| def lookup_list(l): | ||
| return 's' in l | ||
|
|
||
| Even though both functions look identical, because *lookup_dict* is utilizing the fact that dictionaries in python are hashtables, the lookup performance between the two is very different. | ||
| Python will have to go through each item in the list to find a matching case, which is time consuming. By analysing the hash of the dictionary finding keys in the dict can be done very quickly. | ||
| For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_ page. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This all might also fit better in a performance section. |
||
|
|
||
| Zen of Python | ||
| ------------- | ||
|
|
||
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