Flatten using Python yield from

I understand yield, but I came to understand that it is a mechanism to investigate what is yield from and receive the evaluation of the generator. I wrote flatten with that understanding

def flatten(x):
    if hasattr(x, '__iter__') and not isinstance(x, str):
        for y in x:
            yield from flatten(y)
    else:
        yield x

If this yield from is changed to yield, the generator will simply be returned, so I wonder if they understand each other.

In : ls = [1,2,3,[4,5],[6,[7,8,[9,10,11],12]]]

In : list(flatten(ls))
Out: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

I'm not sure if I can handle it

Recommended Posts

Flatten using Python yield from
Using Rstan from Python with PypeR
Python, yield, return, and sometimes yield from
Notes on using MeCab from Python
Using Cloud Storage from Python3 (Introduction)
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Try using Amazon DynamoDB from Python
From Python to using MeCab (and CaboCha)
About python yield
Start using Python
sql from python
I tried using UnityCloudBuild API from Python
MeCab from Python
Scraping using Python
Flatten in python
flatten in python
I want to email from Gmail using Python.
Create wav file from GLSL shader using python3
Operate the schedule app using python from iphone
Run a Python file from html using Django
Load images from URLs using Pillow in Python 3
Run a python script from excel (using xlwings)
Use thingsspeak from python
Touch MySQL from Python 3
Operate Redmine using Python Redmine
Operate Filemaker from Python
Use fluentd from python
Fibonacci sequence using Python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Data analysis using Python 0
Python from or import
Use MySQL from Python
Run python from excel
Install python from source
Execute command from Python
Data cleaning using Python
Operate neutron from Python!
Using Python #external packages
WiringPi-SPI communication using Python
Use MySQL from Python
Age calculation using python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Search Twitter using Python
Name identification using python
Execute command from python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Push notifications from Python to Android using Google's API
MessagePack-Call Python (or Python to Ruby) methods from Ruby using RPC
Installation method when using RealSense from Python (pyenv edition)
Implementing a generator using Python> link> yield and next ()> yield
Copy S3 files from Python to GCS using GSUtil