“Pythonertrag von” Code-Antworten

Pythonertrag von

# In python construction 'yield from something' is just 
# the abbreviation of 'for i in something: yield i'
# So, we can change this:
def yieldOnly():
    yield "A"
    yield "B"
    yield "C"

def yieldFrom():
    for _ in [1, 2, 3]:
        yield from yieldOnly()

test = yieldFrom()
for i in test:
    print(i)
    
# to this:

def yieldOnly():
    yield "A"
    yield "B"
    yield "C"

def yieldFrom():
    for _ in [1, 2, 3]:
        for i in yieldOnly():
            yield i

test = yieldFrom()
for i in test:
    print(i)
Jittery Jay

Ausdrücke in Python ergeben

yield_atom       ::=  "(" yield_expression ")"
yield_expression ::=  "yield" [expression_list | "from" expression]


#Using the yield operation in functions
def gen():  # defines a generator function
    yield 123

async def agen(): # defines an asynchronous generator function
    yield 123
LazFlex

Ähnliche Antworten wie “Pythonertrag von”

Fragen ähnlich wie “Pythonertrag von”

Weitere verwandte Antworten zu “Pythonertrag von” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen