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