raymondhによるPython tips

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

#python survey: Without looking or experimenting, what would you expect "del d[k]" to do where d=defaultdict(int) and k is a missing key?

2011-11-27 14:40:06
Raymond Hettinger @raymondh

#python survey continued: d=defaultdict(int); c=Counter(); print d[7], c[7], len(d), len(c) # What output do you expect?

2011-11-28 04:20:09
Raymond Hettinger @raymondh

#python tip: At its heart, collections.Counter is just: "class Counter(dict): def __missing__(self, key): return 0". The rest is just candy.

2011-11-28 11:42:10
Raymond Hettinger @raymondh

#python lament: Sometimes it's really inconvenient that lists and tuples aren't weak-referenceable

2011-11-30 07:04:10
Raymond Hettinger @raymondh

#python extreme optimization tip: two-way generators can greatly simplify code and allow all state to be localized: http://t.co/jqNqZEWi

2011-11-30 09:59:16
Raymond Hettinger @raymondh

#python history: namedtuple._replace was originally modeled on str.replace. The keyword arguments came in a later iteration.

2011-12-01 03:01:38
Raymond Hettinger @raymondh

#python's int() with base=0 will automatically detect the base: int('42', 0) -> 42 and int('0x2F', 0) -> 47 and int('021', 0) -> 17

2011-12-04 13:26:18
Raymond Hettinger @raymondh

#python's int() has a hidden superpower: int(u'\N{arabic-indic digit four}\N{arabic-indic digit two}') --> 42

2011-12-04 16:32:45
Raymond Hettinger @raymondh

@dnene int(s) converts strings to numbers. The superpower is that it works for digits regardless of language or character set.

2011-12-04 17:55:26
Raymond Hettinger @raymondh

@chrisjrn The \N sequence was just a unicode name lookup for u'\u0664\u0662' which displays as ٤٢.

2011-12-04 18:03:14
前へ 1 ・・ 4 5