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