Last active
February 1, 2021 03:10
-
-
Save dunossauro/a1eb0b8157ff023f0751796b5e8855c9 to your computer and use it in GitHub Desktop.
Tradução do que há de novo (PEP-380)
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 acumular(): | |
""" | |
Toda vez que yield recebe um valor, adiciona ao contador. | |
contador = 0 | |
acumular.send(10) # contador = 10 | |
acumular.send(10) # contador = 20 | |
Quando receber None, retorna o valor final da contagem | |
acumular.send(None) # 20 | |
""" | |
contador = 0 | |
print('Inicializei `acumular`') | |
while True: | |
valor = yield # Recebe um valor | |
print(f'`acumular` recebeu valor={str(valor)}') | |
if valor is None: | |
# Se for None, retorna o valor final da contagem | |
print('Finalizei `acumular`') | |
return contador | |
contador += valor # Caso não seja nulo acumula o valor | |
print(f'`acumular` - Valor total: {contador=}') | |
def agregador_de_contadores(contadores): | |
""" | |
Agregador = gather. | |
Quando iniciada retorna a corotina `acumular` | |
Quando a corrotina retonar um valor irá fazer um append em `contadores` | |
E então retornara uma nova "instancia" de `acumular` | |
""" | |
print('Iniciei `agregador_de_contadores`') | |
while True: | |
print('Deleguei para `acumular`') | |
contador = yield from acumular() | |
contadores.append(contador) | |
print(f'Valor Atual de {contadores=}') | |
contadores = [] | |
agregador = agregador_de_contadores(contadores) # Inicia o gerador delegante | |
next(agregador) # Preparação | |
for i in range(4): | |
# 0 + 1 + 2 + 3 | |
agregador.send(i) # Enviando valores para `acumular` | |
agregador.send(None) # 6 | |
# Finaliza a corrotina `acumular`, com isso `agregador` faz insere o valor final em `contadores` | |
for i in range(5): | |
# 0 + 1 + 2 + 3 + 4 | |
agregador.send(i) | |
agregador.send(None) # 10 | |
for i in range(10): | |
# 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 | |
agregador.send(i) | |
agregador.send(None) # 45 | |
print(contadores) # [6, 10, 45] |
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
$ python exemplo_08.py | |
Iniciei `agregador_de_contadores` | |
Deleguei para `acumular` | |
Inicializei `acumular` | |
`acumular` recebeu valor=0 | |
`acumular` - Valor total: contador=0 | |
`acumular` recebeu valor=1 | |
`acumular` - Valor total: contador=1 | |
`acumular` recebeu valor=2 | |
`acumular` - Valor total: contador=3 | |
`acumular` recebeu valor=3 | |
`acumular` - Valor total: contador=6 | |
`acumular` recebeu valor=None | |
Finalizei `acumular` | |
Valor Atual de contadores=[6] | |
Deleguei para `acumular` | |
Inicializei `acumular` | |
`acumular` recebeu valor=0 | |
`acumular` - Valor total: contador=0 | |
`acumular` recebeu valor=1 | |
`acumular` - Valor total: contador=1 | |
`acumular` recebeu valor=2 | |
`acumular` - Valor total: contador=3 | |
`acumular` recebeu valor=3 | |
`acumular` - Valor total: contador=6 | |
`acumular` recebeu valor=4 | |
`acumular` - Valor total: contador=10 | |
`acumular` recebeu valor=None | |
Finalizei `acumular` | |
Valor Atual de contadores=[6, 10] | |
Deleguei para `acumular` | |
Inicializei `acumular` | |
`acumular` recebeu valor=0 | |
`acumular` - Valor total: contador=0 | |
`acumular` recebeu valor=1 | |
`acumular` - Valor total: contador=1 | |
`acumular` recebeu valor=2 | |
`acumular` - Valor total: contador=3 | |
`acumular` recebeu valor=3 | |
`acumular` - Valor total: contador=6 | |
`acumular` recebeu valor=4 | |
`acumular` - Valor total: contador=10 | |
`acumular` recebeu valor=5 | |
`acumular` - Valor total: contador=15 | |
`acumular` recebeu valor=6 | |
`acumular` - Valor total: contador=21 | |
`acumular` recebeu valor=7 | |
`acumular` - Valor total: contador=28 | |
`acumular` recebeu valor=8 | |
`acumular` - Valor total: contador=36 | |
`acumular` recebeu valor=9 | |
`acumular` - Valor total: contador=45 | |
`acumular` recebeu valor=None | |
Finalizei `acumular` | |
Valor Atual de contadores=[6, 10, 45] | |
Deleguei para `acumular` | |
Inicializei `acumular` | |
[6, 10, 45] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment