Python day17_きょうてい
2112 ワード
きょうていの導入
コプロセッサについて説明するには、反復オブジェクト反復器とジェネレータとは何かを知る必要があります.
反復オブジェクト
反復反復反復:forループを使用して値を取得するプロセスを反復反復可能オブジェクトと呼びます:forループを使用して値を取得できるオブジェクトは反復可能オブジェクト反復可能オブジェクトです:メタグループ、リスト、辞書、文字列、集合、range
オブジェクトが反復可能オブジェクトかどうかを判断する
反復可能オブジェクトと反復
ビルダー
ジェネレータ:特殊な反復器です.つまり、ジェネレータはnext関数とforループを使用して値を取得することができます.方法1:my_generator=(i*2 for i in range(10))print(my_generator)方式2:yieldを使用してdefの中でyieldが生成器の応用を表すことを見る限り:生成器はフィボナッチの数列を完成します
コプロセッサについて説明するには、反復オブジェクト反復器とジェネレータとは何かを知る必要があります.
反復オブジェクト
反復反復反復:forループを使用して値を取得するプロセスを反復反復可能オブジェクトと呼びます:forループを使用して値を取得できるオブジェクトは反復可能オブジェクト反復可能オブジェクトです:メタグループ、リスト、辞書、文字列、集合、range
オブジェクトが反復可能オブジェクトかどうかを判断する
from collections import Iterable #
#
result = isinstance((3, 5), Iterable)
print(" :", result)
result = isinstance([3, 5], Iterable)
print(" :", result)
result = isinstance({"name": "zs"}, Iterable)
print(" :", result)
result = isinstance("hello", Iterable)
print(" :", result)
result = isinstance({2,45,43}, Iterable)
print(" :", result)
result = isinstance(range(3), Iterable)
print("range :", result)
result = isinstance(4, Iterable)
print("int :", result)
result = isinstance(4, int)
print("int int :", result)
## True
# :- isinstance
反復可能オブジェクトと反復
__iter__
: __iter__ __next__
:
# iter : , __iter__
# next : , __next__
ビルダー
ジェネレータ:特殊な反復器です.つまり、ジェネレータはnext関数とforループを使用して値を取得することができます.方法1:my_generator=(i*2 for i in range(10))print(my_generator)方式2:yieldを使用してdefの中でyieldが生成器の応用を表すことを見る限り:生成器はフィボナッチの数列を完成します
def fibnacci(num):
#
a = 0
b = 1
# ,
index = 0
#
while index < num:
result = a
a, b = b, a + b
# 1
index += 1
# yield , ,
params = yield result
print(params)
fib = fibnacci(5)
# result = next(fib)
# print(result)
#
# # # send
# # , return
# result = fib.send("ok")
# print(result)
result = fib.send(None)
print(result)
result = fib.send("xxx")
print(result)
# : send , None, next