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