Home  >  Article  >  Backend Development  >  [python][Basic] Determine whether a variable is iterable

[python][Basic] Determine whether a variable is iterable

高洛峰
高洛峰Original
2017-02-18 10:32:091404browse

Method one:

Applicable to python2 and python3

>>> from collections import Iterable
>>> isinstance("str", Iterable)
True

Method two:

Applicable to python3

s = "hello world"
hasattr(s, "__iter__")


For more articles related to [python][Basics] Determining whether a variable is iterable, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn