raymondhによるPython tips

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

#python tip: The set.discard(k) method unconditionally removes k from a set. For dictionaries, use dict.pop(k, None).

2011-05-03 05:18:53
Raymond Hettinger @raymondh

#python tip: The fast itertools recipes can be copied to a utils module with a single cut-and-paste. http://bit.ly/itertools_plus

2011-05-13 23:54:45
Raymond Hettinger @raymondh

#python tip: Turn an argparse namespace into a regular dictionary by running vars() on the parse_args() result

2011-05-14 03:09:56
Raymond Hettinger @raymondh

#python tip: bytearray(1000) initializes a 1000 byte mutuale array with all values set to zero.

2011-05-23 11:43:31
Raymond Hettinger @raymondh

#python tip: define __instancecheck__ to let a class use new-style duck typing to fool isinstance() http://bit.ly/override_isinstance

2011-05-26 06:40:26
Raymond Hettinger @raymondh

#python tip: use named tuples liberally, almost never index a tuple, either unpack it or use attribute access.

2011-06-02 02:24:17
Brian K. Jones @bkjones

#python tip: namedtuple instances can be passed as '*args', and can be used a la '{0.name}, {0.value}'.format(myobj).

2011-06-03 03:23:50
Raymond Hettinger @raymondh

#python pro tip: Slices with negative steps are hard to grok. Use reversed() instead. s[j-1:i-1:-1] --> list(reversed(s[i:j]))

2011-06-03 08:25:46
Raymond Hettinger @raymondh

#python tip: the % modulo operation in #python differs from some other languages in that a%b will have the same sign as b.

2011-06-06 05:56:53
Raymond Hettinger @raymondh

#python tip: operator.iadd() will add but can't assign: t=(1,2); iadd(t, (3,)) returns (1,2,3) but t is unchanged!

2011-06-13 02:38:14
Raymond Hettinger @raymondh

#python note: operator.iadd() can appear to make assignments: t=[1,2]; iadd(t, [3]) --> [1,2,3] and t will mutate.

2011-06-14 01:14:59
Raymond Hettinger @raymondh

#python tip: Replace unintelligible unicode hex literals with names: int(u'\N{arabic-indic digit four}\N{arabic-indic digit two}')

2011-06-22 19:40:28
Raymond Hettinger @raymondh

#python tip: though functions like sort() and bisect() only call __lt__(), PEP 8 advises you always define all 6 rich comparisons

2011-07-04 05:47:04
Raymond Hettinger @raymondh

#python tip: IDLE's file menu has "open module" and "class browser" which are great for finding and getting an overview of code.

2011-07-05 08:11:44
Raymond Hettinger @raymondh

#python tip: Decimal's unary plus is more useful than it looks. It applies the current context (rounding, limits, etc).

2011-07-07 01:02:40
Raymond Hettinger @raymondh

#python tip: Decimal objects are exact numbers that are independent of context (you can have a 50 digit number in a 20 digit context).

2011-07-07 01:05:14
Raymond Hettinger @raymondh

#python tip: Decimal contexts are applied after or during math operations, not before.

2011-07-07 01:06:51
Raymond Hettinger @raymondh

#python tip: c += Counter() will eliminate all entries with non-positive counts.

2011-07-08 07:19:30
Raymond Hettinger @raymondh

#python tip: When using __slots__, you can add entries for __weakref__ and __dict__ as needed.

2011-07-08 09:10:36
Raymond Hettinger @raymondh

#python tip: some_sorted_dict = OrderedDict(sorted(somedict.items())) # note: need to re-sort if new keys are added

2011-07-09 04:19:09
Raymond Hettinger @raymondh

#python tip: One way to implement the observer design pattern: http://t.co/74CMd6M

2011-07-20 02:50:40
Raymond Hettinger @raymondh

#python tip: collections.deque() takes an optional maxlen argument to automatically pop old elements when news ones are added.

2011-07-28 08:39:17
Raymond Hettinger @raymondh

#python tip: here's a fast, universal constant function to use with collections.defaultdict: f=itertools.repeat(someconst).next

2011-07-29 03:03:17
Raymond Hettinger @raymondh

#python tip: Every time you add __getitem__() indexing to a class, think about adding slicing support too.

2011-08-01 12:22:09
Raymond Hettinger @raymondh

#python tip: whenever a slice has negative variable for the right index, such as s[:-i], I suspect a bug for the case where i==0

2011-08-03 00:17:33
1 ・・ 5 次へ