Python, yield, return, and sometimes yield from

Yield in python

In Python, you can use yield in a function as a Generator. For example

def foo():
    for i in range(10):
       yield i

Written as

for k in foo():
   print(k)

If you write, the value from 0 to 9 is displayed.

Combined yield and return in Python

For example

def hoge():
   for i in range(10):
      yield i
   return i+1

If you do this, you will only get the result of ** yield. ** ** This is a Python specification, and the reason is that return in Generator is treated as StopIteration. Therefore, it is safe to think that it is basically difficult to use yield and return together. Rather, the yield acts as a return within the Generator, so basically It is better to narrow down to either yield or return.

Use together even if you force it

If for some reason you want to use it with return, use yield from to get the value set for return. You will be able to do it. For example

def buzz():
  i = 0
  for j in range(10):
     i += j
     yield j
  return i

If you write a function like this and want to return, prepare a new function separately and do as follows If you write it, you can get up to the value of return.

def getter():
  x = yield from buzz()
  yield x

However, note that yield from is a technique that can only be described within a function.

Summary

--Generator can be generated using yield --Combination of yield and return is strict --You can get up to the value of return by using yield from. --yield from can only be used inside a function --In the end, it's better to focus on yield or return.

Recommended Posts

Python, yield, return, and sometimes yield from
Flatten using Python yield from
Read and use Python files from Python
About Python, from and import, as
[Python] return A [or / and] B
From Python to using MeCab (and CaboCha)
Porting and modifying doublet-solver from python2 to python3.
Python early return
About python yield
sql from python
MeCab from Python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Study from Python Reading and writing Hour9 files
[Python] How to read data from CIFAR-10 and CIFAR-100
[Python] Find coordinates from two angles and distance
Load and execute command from yml in python
PHP and Python integration from scratch on Laravel
Generate and output plantuml object diagram from Python object
[python] Compress and decompress
Use thingsspeak from python
[Python for Hikari-] Chapter 06-04 Functions (arguments and return value 3)
Touch MySQL from Python 3
Python and numpy tips
[Python] pip and wheel
Operate Filemaker from Python
Use fluentd from python
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Batch design and python
Changes from Python 2 to Python 3.0
Python iterators and generators
Python from or import
Python packages and modules
Vue-Cli and Python integration
Use MySQL from Python
Control other programs from Python (communication between Python and exe)
Ruby, Python and map
Run python from excel
Install python from source
[Python beginner] Extract prefectures and cities from addresses (3 lines).
Execute command from Python
Get the return code of the Python script from bat
Operate neutron from Python!
python input and output
Python and Ruby split
Implementing a generator using Python> link> yield and next ()> yield
Use MySQL from Python
Operate LXC from Python
Get mail from Gmail and label it with Python3
Difference between return, return None, and no return description in Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Get files from Linux using paramiko and scp [Python]
[Python for Hikari-] Chapter 06-03 Functions (arguments and return value 2)
Execute command from python
Python3, venv and Ansible
[Python] Extract only numbers from lists and character strings
Python asyncio and ContextVar
[Python] Read From Stdin