Level 1 : What happens in this generator?


def test():
    yield 1
    return 2
    yield 3

g = test()
print(next(g))
print(next(g))

Author: SandhyaR | Tags: iterator | 1859+ Python FAQs | Share
list-mutation | basic | __all__-attribute | counter | built-in-modules | datetime.date,-datetime.time,-datetime.datetime | inheritance | data-structures-in-python | exponentiation | default-arguments | data-type | encapsulation | copy | condition | datetime | file-handling | dictionary-methods | generators | filter-function | generator | complex-number | exception | floating-point-precision | else-statement | error | functions | conversion | anchors-(^,-$) | for | gil | default-mutable-argument | data_structure | compute | match | using-os-and-pathlib-modules | type-conversion | thread-safe-data-structures-(queue.queue) | name-mangling | while-and-for-loop | factorial-function | memory | membership-operators-(in,-not-in) | time-series-with-pandas | logical-operators | sorted | array | decorator | dataframe-and-series-objects | deep-copy | list-method | operator-overloading | directory-operations | managing-directories-with-os-and-shutil | global | regular-expressions | docstring | oops | arithmetic-expression | file-handling | overriding-methods | python-operators | boolean | else | dicts | touples | destructor | type_conversion | encoding | using-lists-as-stacks | overflow | file_handling | map-function | logical-operator | module | list-join | writing:-write(),-writelines() | arbitrary-arguments-(*args,-**kwargs) | regular-expression | expression | syntaxerror | list-methods-and-slicing | control-flow | list-operation | exception-handling | floats | identity-operators-(is,-is-not) | __future__ | pandas | data-structures | mapping-function | input | print-statement | heap-memory | working-with-file-paths | comparison | escape-operator | max-min | set | while-else-loop | immutability | logical-operator | key-value-pairs | quantifiers-(*,-+,-?,-{}) | descriptors | def | while-loops | conditional-statement | frozenset | working-with-binary-files-(rb,-wb) | namespace | loop-control | join | sum-function | sys | static-method | higher-order-function | datatype | center | round | pickling--unpickling | dictionary | string-slicing | filter | oop | magic-method | assignment-operators | booleans | function-and-scope | python-django | debugging | opening-files-(open()) | command-line-parsing | control-flow | data-structure | input-function | binary | using-map()-for-transformations | built-in-function | mathematical-function | identity-operator | pep | attribute | thread-synchronization | reading-and-writing-files | software-design | list-slicing | type-conversion-and-casting | assert | dictionary-comprehensions | min-max-function | python-history | async | walking-through-directories-using-os.walk() | collections | asyncio-module | tuples | working-with-temporary-files-using-tempfile | built-in-function | matrix | typeerror | indexing:-loc-vs-iloc | slice | iterator | loop | memory-management | matrix-list-comprehension | bitwise-operator | data-type | iterable | sorting | multiprocessing | mathematics | classes | rollback | loop-control-statements | logical-operation | math-copysign | input-function | list-comprehensions | inherit | variable | access-modifier | swapping | __init_subclass__ | introduction-to-threads | filtering-and-indexing | expand-tab | round-function | min-function | matplotlib | sets | built-in | list-vs-tuple | id-function | function-argument | namedtuple | error-handling | regex | lambdas | introduction-to-python | multithreading | string-method | data-structure | string-concatenation | objects | formal-and-actual-parameter | zip-function | turtle | loops | pass | string-slicing | append | comment | multiple-inheritance | statement | control-statement | recurssion | difference-between-threads-and-processes | operator | literal-characters-and-special-characters | feature | print | boolean-and-non-boolean | methods | recursion | constructor | package-management | file | mutable | single-inheritance | multiple-inheritance | functional-programming | python-repl | strings | shallow-copy | version-check | integers | complex | sorting-and-ranking | sum() | random | syntax | assignment-operator | update | indexing | staticmethod | list | invalid-list | string | design-principle | string-manipulation | installation | class-and-object | unit-testing | python-module | sys-module | asyncio | range | lambda-functions | creating-custom-modules | using-the-threading-module | list-comprehension | comprehension | context-manager | dictionaries | operator-or | with-statement | method-overloading | list-indexing | tuple-methods | enumerate() | interpreter | zip | implementation | tuple-unpacking | call | application | list,-set,-and-dictionary-comprehensions | conditional-statement | using-else-and-finally | syntax-error | creating-and-starting-threads | list-comprehension | precedence-and-associativity | iterators | set-operation | scientific-notation | operator-and | counter-function | operand | yield | using-join()-and-is_alive()-methods | lists | mysql | decimal | why-use-multiprocessing-over-threading?-(gil) | functional-programming | arithmetic-operation | string-methods | tuple | formatting-with-strftime | threading | method | return-statement | exception-handling | conditional-statements | string-formatting | defining-functions | math | boolean-masking | operators | head-and-tail | data-types | goto-function | python | odd-number | data-type-conversions | zfill-function | version | benefit | joining-and-merging-dataframes | global-variable | string-method | raise-statement | identity-operator | split | class-decorators | raise | global-keyword | collection | parameter | bitwise | function | postfix | python_version | class-and-inheritance | working-with-dates-and-times | slicing | precedence | nested-loop | binary-operation | string-iteration | integer | class-and-instance-variable | python-syntax-and-structure | format | virtual-environment | advanced-file-operations | operations | init | using-yield-in-functions | class | object-attribute | reading-and-writing-large-files-efficiently | while-loop | random-function | immutable-data-type | type-conversion | import-method | polymorphism | indentation | choice | aggregating-data-with-functools.reduce() | virtual-environment | fabs-function | intering | reading:-read(),-readline(),-readlines() | test-discovery | generators-in-python | syntax-error | string-formatting | global-variable | stack | parsing-with-strptime | int | error-handling | lambda-function | library | thread | keyword | introduction-to-multiprocessing | numeric | pseudocode | handling-missing-values-(fillna,-dropna) | identifier | using-assertions-(assertequal,-assertraises) | bitwise-operator | numpy | lambda | multiple-except-blocks ...