by shigemk2

当面は技術的なことしか書かない

2013-07-20から1日間の記事一覧

エキスパートPythonプログラミング デコレータ その2

元の関数をラップするデコレータを使用した場合、関数名やdocstringがデコレータで定義したものに 置き換わってしまう。 >>> def mydecorator(function): ... def _mydecorator(*args, **kw): ... res = function(*args, **kw) ... return res ... return _m…

エキスパートPythonプログラミング デコレータ

# before 2.3 class WhatFor(object): def it(cls): print ('work with %s' % cls) it = classmethod(it) # make it classmethod def uncommon(): print ('I could be a global function') uncommon = staticmethod(uncommon) # make it staticmethod whatfo…

エキスパートPythonプログラミング ウィンドウイテレータ

isliceはシーケンスのサブグループに対するイテレータを返す。 次の例は、標準入力から行単位で入力を読み込み、その行が5つ以上の要素を 持つ場合に5番目の要素から順にyieldする Python 2.7.4 (default, Apr 18 2013, 22:40:51) [GCC 4.2.1 Compatible App…

エキスパートPythonプログラミング ジェネレータ式

リスト内包表記と似た表現。 yieldを使ったシンプルなループや、イテレータのように動作するリスト内包表記は、積極的にジェネレータ式に置き換えるべきである。 iter = (x**2 for x in range (10) if x % 2 == 0) for el in iter: print (el) 実行結果 0 4 …

エキスパートPythonプログラミング コルーチン

関数の一種で、複数の箇所で実行を一時停止したり、再開したりすることが出来るなお、multitaskはPython2系でしか使えない import multitask def coroutlime_1(): for i in range(3): print 'c1' yield i def coroutlime_2(): for i in range(3): print 'c2'…

JavaScript Ninjaの極意 4 メモ化

自分が計算した値を覚えておくように関数を構築する。 <html> <head> <meta charset="utf-8"> <title>Listing 4.9</title> <script type="text/javascript" src="../scripts/assert.js"></script> <link href="../styles/assert.css" rel="stylesheet" type="text/css"> </head> <body> <script type="text/javascript"> function is…</body></html>