みなさん、こんにちは!独学で Python を勉強していたとき、言語とその機能に関する理論的な資料を十分に見つけました。しかし、さまざまなサイトや本でいくつかの記事を読んでも、多くのことが頭に収まりませんでした (はい、とてもタイトです)。記事の中の実際の例は私にとって難しかったため、理解できない概念は、深い理解なしに「信仰に基づいて」詰め込まれなければなりませんでした。時が経ち、私はより経験を積むようになり、実践的な問題について理解が深まり、ある時点で私は友人に Python を教え始めました。メンタリングを通じて、私は複雑な概念を簡単な言葉で説明できる道筋を示しているように感じました。
こどもの日のITコミュニティ全体について、そして初心者が一見難しくて理解できないものの美しさと利点を理解するのに役立つことを願って、私はデビュー記事を書いています.
今日はまた発電機について話したいと思います。というわけでバトル開始!
概念を理解し、タスクを設定します
誰かが Python ジェネレーターについて話すときに常に念頭に置いておくべきことは、「内包表記」と「イテレーター ジェネレーター」を混同しないことです。前者はその場でコレクションを生成するための強力な糖衣構文であり、後者はオンデマンドで値を取得する方法です。今日は2番目に焦点を当てます。
同じおとぎ話の中で、同じ少年 Vovka が次のように言ったと想像してみましょう。その少年だけが長い間行方不明になっていて、何人かの賢者ヴァシリーサは、帰り道と引き換えに私たちに仕事を与えてくれます。
目的: 自己組織化されたテーブルクロスを使用して 3 人の実験対象者に食事を与える定期的 (たとえば、毎日、毎時など) のプロセスを編成する必要があります。自己組織化されたテーブルクロスは、その「クリップ」に毎回異なる数の食品を入れています。被験者のそれぞれにおける空腹感は、(1つの現時点では、被験者は、10の3のために空腹感を経験するかもしれない、といくつかの時間後にも同様に矛盾している - すべての10のための食糧をしたいです)。
: , , . . , , , .
, :
. .
. .
. : , - - . , (-, , ) , - . , , . . , :)
:
, , , . , . «», , , , - . .
:
- |
||
20 . 40 . |
30 . 50 . |
25 . 5 . |
15 . 35 . |
10 . 25 . |
30 . 35 . |
30 . 50 . |
20 . 15 . |
15 . 15 . |
Python. - . 0 100 000 000.
859 724 472 , 6 . , - , . , , . . : . ?
«» . , n , - . - -. , - . , .
, , , — . : next
, .
, , . , . , , , . , .
, , next: ,
: , 100 000 000 . 2 , — 10, (5) (3). «».
, next , -. - 100 000 000 120 ( — ), 0,00009 . , , , ( — ), .
:
- , , - . , , .
- , , . , . .
Python. , - , 0 n.
, (). , - . , 4, 2 . .
def skat(n):
""", 0 n"""
# range, range -.
res = []
cnt = 0
while cnt < n:
res.append(cnt)
cnt += 1
return res
def first_eater(eda_list):
""" """
for eda in eda_list:
print(f" {eda} : ", str(eda))
def second_eater(eda_list):
""" """
for eda in eda_list:
print(f" {eda} : ", str(eda) * 4)
def third_eater(eda_list):
""" """
for eda in eda_list:
print(f" {eda} : ", str(eda) * 10)
#
eda_list = skat(100_000_000)
#
golod_1 = 2
golod_2 = 3
golod_3 = 4
#
first_eater(eda_list[:golod_1])
second_eater(eda_list[golod_1:golod_2 + golod_1])
third_eater(eda_list[golod_2 + golod_1:golod_2 + golod_1 + golod_3])
# , :
# >>> 0 : 0
# >>> 1 : 1
# >>> 2 : 2222
# >>> 3 : 3333
# >>> 4 : 4444
# >>> 5 : 5555555555
# >>> 6 : 6666666666
# >>> 7 : 7777777777
# >>> 8 : 8888888888
, : . , , . . , , . , , , pop
, ( ), .
, , — . ? . - (return
- ) ( return
None
). , Python return
, — yield
.
, :
def my_func_1():
print(" ")
return 1
def my_func_2():
print(" ")
yield 1
( , , ):
print(my_func_1)
print(my_func_2)
# , :
# >>> <function my_func_1 at 0x10c399950>
# >>> <function my_func_2 at 0x10c3a4290>
, my_func_1
my_func_2
, . . , :
print(my_func_1())
print(my_func_2())
# ,
# >>>
# >>> 1
# >>> <generator object my_func_2 at 0x108a702d0>
« ?» — .
« !» — .
, , yield
, - (generator-object
). , - — , , . ! , -, my_func_2
.
? ! Python next
( , ?), - ( -, , ) yield
, yield
. :
print(next(my_func_2()))
# , :
# >>> 1
! ! «... yield
...»? , , , - , yield
! - my_func_2
yield
:
def my_func_2():
print(" ")
yield 1
print(" ")
yield 2
print(" !")
# :
gen_o = my_func_2() # generator-object
print(next(gen_o))
print(", - !")
print(next(gen_o))
# , :
# >>>
# >>> 1
# >>> , - !
# >>>
# >>> 2
, generator-object
next
yield
. , next
. «», . , , generator-object
, . ! .
next
:
gen_o = my_func_2()
print(next(gen_o))
print(", - !")
print(next(gen_o))
print(next(gen_o))
# , :
# >>> 1
# >>> , - !
# >>>
# >>> 2
# >>> !
# >>> Traceback (most recent call last):
# >>> File "< >", line 13, in <module>
# >>> print(next(gen_o))
# >>> StopIteration
, «» generator-object
. , yield
, . - , , StopIteration
.
: generator-object
«» . - «» . . generator-object
, my_func_2
.
. , , . , n, . , generator-object
, -, :
def skat(n):
""", -,
0 n"""
cnt = 0
while cnt < n:
yield cnt
cnt += 1
def first_eater(golod, skat):
""" """
while golod > 0:
eda = next(skat)
print(f" {eda} : ", eda)
golod -= 1
def second_eater(golod, skat):
""" """
eda = next(skat)
while golod > 0:
print(f" {eda} : ", str(eda) * 4)
golod -= 1
def third_eater(golod, skat):
""" """
eda = next(skat)
while golod > 0:
print(f" {eda} : ", str(eda) * 10)
golod -= 1
skat_gen_obj = skat(100_000_000)
golod_1 = 2
golod_2 = 3
golod_3 = 4
try:
first_eater(golod_1, skat_gen_obj)
second_eater(golod_2, skat_gen_obj)
third_eater(golod_3, skat_gen_obj)
except StopIteration:
print(" !")
# , :
# >>> 0 : 0
# >>> 1 : 1
# >>> 2 : 2222
# >>> 2 : 2222
# >>> 2 : 2222
# >>> 3 : 3333333333
# >>> 3 : 3333333333
# >>> 3 : 3333333333
# >>> 3 : 3333333333
try - except
, . , , , .
«» ( , ). , , , . , , , (, «» :D) — generator-object
.
- . , .
- . .
- , , .
, :
,
, ,
, :
,
- — , !
, :
, ( ) , , ,
: , , ..
, ! , , — ). - , for
, .