SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
PythonのYieldを
かじってみた
@24motz
2014-05-03 LT駆動開発03
Pythonの関数
Python 2.7.6 (default, Nov 18 2013, 15:12:51)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
!
>>> def a():
... print "hello"
...
>>> a()
hello
>>> type(a)
<type 'function'>
yieldを含む関数
>>> def hoge():
... yield
...
>>> type(hoge)
<type 'function'>
>>> hoge()
<generator object hoge at 0x1019fda00>
>>> g = hoge()
>>> type(g)
<type 'generator'>
!
yieldを含む関数は

イテレータと同じインタフェースを持つ
呼び出し可能オブジェクト
(ジェネレーター)を返す
イテレーター
>>> def hoge():
... yield 'a'
... yield 'b'
...
>>> g = hoge()
>>> print g.next()
a
>>> print g.next()
b
>>> print g.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
シーケンス生成
>>> def hoge():
... yield 'a'
... yield 'b'
...
>>> g = hoge()
>>> for i in g:
... print i
...
a
b
sendメソッド
>>> def hoge():
... s = (yield 'a')
... s = (yield s + 'b')
... s = (yield s + 'c')
...
>>> g = hoge()
>>> print g.send(None)
a
>>> print g.send('x')
xb
>>> print g.send('y')
yc
>>> print g.send('z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
呼出し側からジェネレータに

値を渡すことができる
ただし
最初の呼び出しは値を渡せない
!
send(None) は next() でも可
最初だけnext()なのは気持ち悪い
状態遷移モデル
def stateManagerGenerator():
while True:
ev = (yield State.FIRST)
if ev == Event.GOTO_SECOND:
break
while True:
ev = (yield State.SECOND)
if ev == Event.GOTO_END:
break
while True:
ev = (yield State.END)
!
sm = stateManagerGenerator()
state = sm.send(None) #=> FIRST
state = sm.send(Event.DUMMY) #=> FIRST
state = sm.send(Event.GOTO_SECOND) #=> SECOND
受け取ったevに応じて
次の状態に進む
状態遷移の規則を
ひとつの関数に
まとめられる

Mais conteúdo relacionado

Mais procurados

金曜だけどデザインパターンでも勉強しよう
金曜だけどデザインパターンでも勉強しよう金曜だけどデザインパターンでも勉強しよう
金曜だけどデザインパターンでも勉強しよう
Takaaki Hirano
 
Perlで伝統芸能
Perlで伝統芸能Perlで伝統芸能
Perlで伝統芸能
hitode909
 
10min r study_tokyor25
10min r study_tokyor2510min r study_tokyor25
10min r study_tokyor25
Nobuaki Oshiro
 
すごいHaskell読書会#10
すごいHaskell読書会#10すごいHaskell読書会#10
すごいHaskell読書会#10
Shin Ise
 
G*workshop 2011/11/22 Geb+Betamax
G*workshop 2011/11/22 Geb+BetamaxG*workshop 2011/11/22 Geb+Betamax
G*workshop 2011/11/22 Geb+Betamax
Nobuhiro Sue
 

Mais procurados (19)

asm.js x emscripten: The foundation of the next level Web games
asm.js x emscripten: The foundation of the next level Web gamesasm.js x emscripten: The foundation of the next level Web games
asm.js x emscripten: The foundation of the next level Web games
 
金曜だけどデザインパターンでも勉強しよう
金曜だけどデザインパターンでも勉強しよう金曜だけどデザインパターンでも勉強しよう
金曜だけどデザインパターンでも勉強しよう
 
Perlで伝統芸能
Perlで伝統芸能Perlで伝統芸能
Perlで伝統芸能
 
10min r study_tokyor25
10min r study_tokyor2510min r study_tokyor25
10min r study_tokyor25
 
for文
for文for文
for文
 
入力
入力入力
入力
 
Local php-100828 2
Local php-100828 2Local php-100828 2
Local php-100828 2
 
Good Parts of PHP and the UNIX Philosophy
Good Parts of PHP and the UNIX PhilosophyGood Parts of PHP and the UNIX Philosophy
Good Parts of PHP and the UNIX Philosophy
 
while文
while文while文
while文
 
[TL06] 日本の第一人者が C# の現状と今後を徹底解説! 「この素晴らしい C# に祝福を!」
[TL06] 日本の第一人者が C# の現状と今後を徹底解説! 「この素晴らしい C# に祝福を!」[TL06] 日本の第一人者が C# の現状と今後を徹底解説! 「この素晴らしい C# に祝福を!」
[TL06] 日本の第一人者が C# の現状と今後を徹底解説! 「この素晴らしい C# に祝福を!」
 
ひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指すひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指す
 
C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~
C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~
C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~
 
S02 t1 sta_py_tsuji_0702_slides
S02 t1 sta_py_tsuji_0702_slidesS02 t1 sta_py_tsuji_0702_slides
S02 t1 sta_py_tsuji_0702_slides
 
実用裏方 Perl 入門
実用裏方 Perl 入門実用裏方 Perl 入門
実用裏方 Perl 入門
 
すごいHaskell読書会#10
すごいHaskell読書会#10すごいHaskell読書会#10
すごいHaskell読書会#10
 
Stroustrup11章雑感
Stroustrup11章雑感Stroustrup11章雑感
Stroustrup11章雑感
 
G*workshop 2011/11/22 Geb+Betamax
G*workshop 2011/11/22 Geb+BetamaxG*workshop 2011/11/22 Geb+Betamax
G*workshop 2011/11/22 Geb+Betamax
 
F#のすすめ
F#のすすめF#のすすめ
F#のすすめ
 
Teclab3
Teclab3Teclab3
Teclab3
 

Semelhante a Python yield

エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7
Tetsuya Morimoto
 
Python東海GAEやってみた
Python東海GAEやってみたPython東海GAEやってみた
Python東海GAEやってみた
Mori Shingo
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
Akira Takahashi
 
Python Kyoto study
Python Kyoto studyPython Kyoto study
Python Kyoto study
Naoya Inada
 
Hello World Python featuring GAE
Hello World Python featuring GAEHello World Python featuring GAE
Hello World Python featuring GAE
Maito Kuwahara
 
Sohu邮箱的python经验
Sohu邮箱的python经验Sohu邮箱的python经验
Sohu邮箱的python经验
Ryan Poy
 

Semelhante a Python yield (20)

エキ Py 読書会02 2章後半
エキ Py 読書会02 2章後半エキ Py 読書会02 2章後半
エキ Py 読書会02 2章後半
 
エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7
 
Easy caching and logging package using annotation in Python
Easy caching and logging package using annotation in PythonEasy caching and logging package using annotation in Python
Easy caching and logging package using annotation in Python
 
Heap statsfx analyzer
Heap statsfx analyzerHeap statsfx analyzer
Heap statsfx analyzer
 
Python東海GAEやってみた
Python東海GAEやってみたPython東海GAEやってみた
Python東海GAEやってみた
 
2017/12/21 虎の穴 Python勉強会
2017/12/21 虎の穴 Python勉強会2017/12/21 虎の穴 Python勉強会
2017/12/21 虎の穴 Python勉強会
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
 
Everyday Life with clojure.spec
Everyday Life with clojure.specEveryday Life with clojure.spec
Everyday Life with clojure.spec
 
エキ Py 読書会02 2章前半
エキ Py 読書会02 2章前半エキ Py 読書会02 2章前半
エキ Py 読書会02 2章前半
 
Python Kyoto study
Python Kyoto studyPython Kyoto study
Python Kyoto study
 
Hello World Python featuring GAE
Hello World Python featuring GAEHello World Python featuring GAE
Hello World Python featuring GAE
 
Node予備校 vol.1 名古屋
Node予備校 vol.1 名古屋Node予備校 vol.1 名古屋
Node予備校 vol.1 名古屋
 
Sohu邮箱的python经验
Sohu邮箱的python经验Sohu邮箱的python经验
Sohu邮箱的python经验
 
Introduction of Python
Introduction of PythonIntroduction of Python
Introduction of Python
 
社内向けTech Talk資料~Fluentdの基本紹介~
社内向けTech Talk資料~Fluentdの基本紹介~ 社内向けTech Talk資料~Fluentdの基本紹介~
社内向けTech Talk資料~Fluentdの基本紹介~
 
イベント駆動プログラミングとI/O多重化
イベント駆動プログラミングとI/O多重化イベント駆動プログラミングとI/O多重化
イベント駆動プログラミングとI/O多重化
 
Ajax 応用
Ajax 応用Ajax 応用
Ajax 応用
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
第3回Webkit/HTML5勉強会 - File APIと加速度センサー
第3回Webkit/HTML5勉強会 - File APIと加速度センサー第3回Webkit/HTML5勉強会 - File APIと加速度センサー
第3回Webkit/HTML5勉強会 - File APIと加速度センサー
 
EC-CUBEプラグイン講義
EC-CUBEプラグイン講義EC-CUBEプラグイン講義
EC-CUBEプラグイン講義
 

Mais de Takuya Nishimoto

Mais de Takuya Nishimoto (20)

221217 SwiftはPythonに似ている
221217 SwiftはPythonに似ている221217 SwiftはPythonに似ている
221217 SwiftはPythonに似ている
 
220427-pydata 統計・データ分析 特集
220427-pydata 統計・データ分析 特集220427-pydata 統計・データ分析 特集
220427-pydata 統計・データ分析 特集
 
220126 python-datalake-spark
220126 python-datalake-spark220126 python-datalake-spark
220126 python-datalake-spark
 
211120 他人の書いたPythonスクリプトをステップ実行で理解する
211120 他人の書いたPythonスクリプトをステップ実行で理解する211120 他人の書いたPythonスクリプトをステップ実行で理解する
211120 他人の書いたPythonスクリプトをステップ実行で理解する
 
211020 すごい広島 with OSH 2021.10
211020 すごい広島 with OSH 2021.10211020 すごい広島 with OSH 2021.10
211020 すごい広島 with OSH 2021.10
 
210917 オープンセミナー@広島のこれまでとこれから
210917 オープンセミナー@広島のこれまでとこれから210917 オープンセミナー@広島のこれまでとこれから
210917 オープンセミナー@広島のこれまでとこれから
 
210911 これから始める電子工作とMicroPython
210911 これから始める電子工作とMicroPython210911 これから始める電子工作とMicroPython
210911 これから始める電子工作とMicroPython
 
210728 mpy
210728 mpy210728 mpy
210728 mpy
 
210630 python
210630 python210630 python
210630 python
 
210526 Power Automate Desktop Python
210526 Power Automate Desktop Python210526 Power Automate Desktop Python
210526 Power Automate Desktop Python
 
210428 python
210428 python210428 python
210428 python
 
200918 hannari-python
200918 hannari-python200918 hannari-python
200918 hannari-python
 
200429 python
200429 python200429 python
200429 python
 
200325 flask
200325 flask200325 flask
200325 flask
 
200208 osh-nishimoto-v2
200208 osh-nishimoto-v2200208 osh-nishimoto-v2
200208 osh-nishimoto-v2
 
191208 python-kansai-nishimoto
191208 python-kansai-nishimoto191208 python-kansai-nishimoto
191208 python-kansai-nishimoto
 
191101 nvda-sightworld-nishimoto
191101 nvda-sightworld-nishimoto191101 nvda-sightworld-nishimoto
191101 nvda-sightworld-nishimoto
 
191114 iotlt-nishimoto
191114 iotlt-nishimoto191114 iotlt-nishimoto
191114 iotlt-nishimoto
 
191030 anna-with-python
191030 anna-with-python191030 anna-with-python
191030 anna-with-python
 
190916 nishimoto-nvda-pyconjp
190916 nishimoto-nvda-pyconjp190916 nishimoto-nvda-pyconjp
190916 nishimoto-nvda-pyconjp
 

Último

Último (11)

知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 

Python yield

  • 2. Pythonの関数 Python 2.7.6 (default, Nov 18 2013, 15:12:51) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. ! >>> def a(): ... print "hello" ... >>> a() hello >>> type(a) <type 'function'>
  • 3. yieldを含む関数 >>> def hoge(): ... yield ... >>> type(hoge) <type 'function'> >>> hoge() <generator object hoge at 0x1019fda00> >>> g = hoge() >>> type(g) <type 'generator'> ! yieldを含む関数は
 イテレータと同じインタフェースを持つ 呼び出し可能オブジェクト (ジェネレーター)を返す
  • 4. イテレーター >>> def hoge(): ... yield 'a' ... yield 'b' ... >>> g = hoge() >>> print g.next() a >>> print g.next() b >>> print g.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
  • 5. シーケンス生成 >>> def hoge(): ... yield 'a' ... yield 'b' ... >>> g = hoge() >>> for i in g: ... print i ... a b
  • 6. sendメソッド >>> def hoge(): ... s = (yield 'a') ... s = (yield s + 'b') ... s = (yield s + 'c') ... >>> g = hoge() >>> print g.send(None) a >>> print g.send('x') xb >>> print g.send('y') yc >>> print g.send('z') Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration 呼出し側からジェネレータに
 値を渡すことができる ただし 最初の呼び出しは値を渡せない ! send(None) は next() でも可 最初だけnext()なのは気持ち悪い
  • 7. 状態遷移モデル def stateManagerGenerator(): while True: ev = (yield State.FIRST) if ev == Event.GOTO_SECOND: break while True: ev = (yield State.SECOND) if ev == Event.GOTO_END: break while True: ev = (yield State.END) ! sm = stateManagerGenerator() state = sm.send(None) #=> FIRST state = sm.send(Event.DUMMY) #=> FIRST state = sm.send(Event.GOTO_SECOND) #=> SECOND 受け取ったevに応じて 次の状態に進む 状態遷移の規則を ひとつの関数に まとめられる