Level 9 : What happens when we send() values into a generator?


def accumulator():
    total = 0
    while True:
        value = yield total
        if value is None:
            break
        total += value

gen = accumulator()
print(next(gen))
print(gen.send(10))
print(gen.send(20))
print(gen.send(None))

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