1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonをストレスなく使う!(generatorに詳しくなる2。defで定義できるのは。。。ちょっと。)

Posted at

目的

 pythonをストレスなく使う!
そのためには、少しでも、理解のレベルを上げる必要あり。
なんでも、こだわって、、、、理解を深める。
ここで記載しているのは、
**generatorに詳しくなる2。defで定義できるのは。。。ちょっと。**です。

pythonを少し勉強はじめて、1年弱、経過。
ほとんど、コードを書いていないので、あまり、レベルアップしていない。
いまだ、
generatorの理解が浅い。

以前、

pythonをストレスなく使う!(generatorに詳しくなる。since1975らしい。)

に、

関数内に、「yield」があるだけで、generator関数になる。
なんという、、、定義のされ方、処理方法の変化の大きさ!

と自分で記載しているが、すっかり、忘れていた。

今回、記事を書くきっかけ

どこかのサイトで、generatorは、クラスに近いという記載をみかけた。
確かに。。。。
と思ったので。

defで定義できるのは。。。ちょっと。

「入門 Python3」オライリー・ジャパン
に以下のような説明のコードがある。

(出典: 「入門 Python3」オライリー・ジャパン )

>>> def my_range(first=0, last=10, step=1):
...    number = first
...    while number < last:
...        yield number
...        number += step
...
>>> my_range
<function my_range at 0x00000242D74720D0>
>>> ranger = my_range(1, 5)
>>> ranger
<generator object my_range at 0x00000242D70D19A8>
>>>
>>> for x in ranger:
...     print(x)
...
1
2
3
4
>>>

以下のあたりが、パンチが強すぎですね。。。
yieldを見落としたら、混乱しますね。。。。

defで定義できるのは。。。
確かに、クラスに近い <--これが、今回の記事記載の目的

>>> ranger = my_range(1, 5)
>>> ranger
<generator object my_range at 0x00000242D70D19A8>
>>>

まとめ

generatorをうまく使おうと思います。

関連(本人)

直接関係するもの

pythonをストレスなく使う!(generatorに詳しくなる。since1975らしい。)

直接はあまり関係ないもの

pythonをストレスなく使う!(Pythonでは、すべてがオブジェクトとして実装されている)
pythonをストレスなく使う!(Pylintに寄り添う)
pythonをストレスなく使う!(ExpressionとStatement)
英語と日本語、両方使ってPythonを丁寧に学ぶ。

今後

コメントなどあれば、お願いします。:candy:
勉強します、、、、
(勉強というより、コードを書くスキルを身に着けたほうがいい気がしてます。)

1
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?