Level 10 : What is the output?


def gen():
    try:
        yield 1
        yield 2
        yield 3
    except GeneratorExit:
        print("Generator closed")

g = gen()
print(next(g))
g.close()
print(next(g))

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