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