Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/writing/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.::
Copy link
Copy Markdown
Contributor

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


letters = ['s', 'p', 'a', 'm']
word = ''.join(letters)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 += when you're getting chunks of string data from something like a socket, file, template engine, etc, not from a list literal. That might be too complex to convey in the style section, perhaps we need to add a performance tricks area? Could be in recipe format:

Problem

You receive bits of data from one or more sources, but need to concatenate this into a single string for a return value

Solution

Use the string.join method, like:

parts = []
for part in thing_that_gives_strings():
    parts.append(part)
out = ''.join(parts)

(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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And generators and anything iterable that gives strings, like ''.join(line for line in file('the_filename.txt')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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:
chars = "".join(f)

which is a silly version of: chars = f.read()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
-------------
Expand Down