Created
July 10, 2011 16:29
-
-
Save oyakata/1074668 to your computer and use it in GitHub Desktop.
ジェネレータ式は引数が一つでない場合は括弧を付けなければなりません -- ただの英語の勉強です キリッ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def spam(spams): | |
for s in spams: | |
print "spam(%s)" % s | |
def ham(hams, num): | |
for h in hams: | |
print "ham(%s)" % (h + num) | |
spam(x + 3 for x in range(11)) | |
ham(x + 3 for x in range(11), 9) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "bin/py", line 62, in <module> | |
execfile(__file__) | |
File "genexp_sample.py", line 10 | |
ham(x + 3 for x in range(11), 9) | |
SyntaxError: Generator expression must be parenthesized if not sole argument | |
spamは引数が一つなのでエラーになりません。 | |
しかし、hamは引数が二つなのでこの通りエラーになります。 | |
>>> | |
https://twitter.com/#!/shimizukawa/status/90092784836550656 | |
@imagawa_yakata エキPy読んでね(はぁと) Tips: ジェネレータ式の()は1引数の関数に渡す場合に限り省略できます。 | |
<<< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
もう笑っちゃうね。「しね」って言われているようなもんですよwww """yieldを使用したシンプルなループや、イテレータのように動作するリスト内包表記は、積極的にジェネレータ式に置き換えるべきです。 --エキPy(初版) 72ページ """