raymondhによるPython tips

raymondhが時々ツイートするPython tipがためになるのでまとめてみました。
1
Raymond Hettinger @raymondh

The persistent dictionary recipe has been updated to support context managers and to run in Py3.x: http://t.co/ygwIee6H

2011-09-16 02:41:10
Raymond Hettinger @raymondh

#python tip: frozensets are hashable so you can use them as dict keys or as members of another set. This is handy for graph algorithms.

2011-09-16 16:28:46
Raymond Hettinger @raymondh

#python3 note: 1 and None cannot be compared in Python 3, so lists with missing values need to be filtered before sorting.

2011-09-18 17:12:23
Raymond Hettinger @raymondh

What can a #python list do that a tuple can't? set(dir(list)) - set(dir(tuple))

2011-09-21 10:45:13
Raymond Hettinger @raymondh

#python proves euler wrong ;-) math.e ** (1j * math.pi) + 1 != 0

2011-09-25 08:30:28
Raymond Hettinger @raymondh

#python tip: subprocess.check_output() is easy to learn, easy to use, and more than a little empowering.

2011-10-04 12:00:24
Raymond Hettinger @raymondh

#python tip: Testing the result of str.find() seems to be a never ending source of bugs. Use with care or use str.index() instead.

2011-10-05 22:46:30
Raymond Hettinger @raymondh

#python tip from Larry Hastings: shlex.split() works great with subprocess.Popen() and subprocess.check_output()

2011-10-13 05:36:36
Raymond Hettinger @raymondh

#python tip: Rather than making multiple pickle.dumps to a single file, just combine the data into one list: dump([a,b], f)

2011-10-14 04:27:36
Raymond Hettinger @raymondh

#python tip: For scripts that don't need "import site", python can be launched with the -S option to improve start-up time.

2011-10-14 22:35:54
Raymond Hettinger @raymondh

#python tip: It is vital to use parentheses when catching multiple exceptions. "except IndexError, KeyError:" is a grievous error.

2011-10-17 04:06:29
Raymond Hettinger @raymondh

else-clauses in #python's for/while loops are trivially easy to explain but some minds just rebel at the thought: http://t.co/HUhn1GrG

2011-10-19 01:30:14
Raymond Hettinger @raymondh

Simple #python idiom for splitting sequences into chunks: [s[i: i+4096] for i in xrange(0, len(s), 4096)]

2011-10-19 06:51:17
Raymond Hettinger @raymondh

Beginner #python? print '68656c6c6f20776f726c64'.decode('hex')

2011-10-20 01:22:50
Raymond Hettinger @raymondh

#python tip: A handy and commonly over-looked builtin function is vars(). Works on classes, instances, modules, argparse namespaces, ...

2011-10-20 04:14:29
Raymond Hettinger @raymondh

@alex_gaynor Instances with __slots__ don't have a dictionary so there's nothing for vars() to get.

2011-10-20 04:47:44
Raymond Hettinger @raymondh

#python tip: Linux/Mac users should open binary files in the "rb" mode. That will avoid unpleasantness on Windows and on Py3.x

2011-10-20 09:03:52
Raymond Hettinger @raymondh

The new HTTP response codes are great. http://t.co/DLixJ28d My favorite: 429 Too Many Requests

2011-10-21 02:17:40
Raymond Hettinger @raymondh

#python tip: Learn to use double underscore name mangling for private intraclass calls: http://t.co/9r5qrFWG

2011-10-21 10:02:52
Raymond Hettinger @raymondh

I was curious how often people use #python's open() in a for-loop, relying on implicit closing: http://t.co/YVvdj3vT

2011-10-21 17:16:20
Raymond Hettinger @raymondh

#python library of the day: http://t.co/1Jlvuxii Easy to use symbolic equation solver in pure python (with extensive tests and comments).

2011-10-22 06:01:30
Raymond Hettinger @raymondh

#python teaching anti-pattern: Student: how does this work? Teacher: it works like this. Heckler: but that doesn't work in Py3.x ...

2011-10-26 04:11:24
Raymond Hettinger @raymondh

#python tip: If you don't want sqlite3 to return unicode strings, set conn.text_factory=str

2011-10-28 20:47:01