Skip to main content

Module that provides the isiterable function to check if an object is iterable quickly for code in production.

Project description

isiterable

isiterable is a Python module that provides the isiterable function, which acts as an object checker to know if it is iterable or not. It does not call the function and also has no undesirable performance numbers.

Features of isiterable

  • Never call __iter__
  • Functional for generators
  • It's faster than tryiter or hasiter

Installation

pip install --upgrade isiterable

Usage

from isiterable import isiterable


print(isiterable(57890))  # False
print(isiterable([1, "Hello!"]))  # True

Using with classes

from isiterable import isiterable


class NoIters:
    def __init__(self, *args):
        self.args = args

    def __iter__(self):
        # isiterable will return False, Must be a generator :/
        return self.args


class YesIters:
    def __init__(self, *args):
        self.args = args

    def __iter__(self):
        # Works!
        for arg in args:
            yield arg


x = YesIters((1, 2))
res = isiterable(x)
print(res)  # True

x = NoIters("Hello!")
res = isiterable(x)
print(res)  # False

Other Examples

>>> from isiterable import isiterable
>>> isiterable(b"\x00\x01...")
True
>>> isiterable(callable)
False
>>> isiterable({"key1": True})
True
>>> isiterable([])
True

Alternatives to isiterable

If it doesn't match using isiterable, there are some solutions where you know which is better.

try-iter

This involves calling __iter__

try:
    iter(object)
except TypeError:
    pass

has-iter

this will check if the object has the attribute __iter__

if hasattr(object, "__iter__"):
    # ...

Benchmark

This small benchmark uses 3 code blocks:

Where object is the argument to pass

Running benchmark.py will give results similar to this

Running 1,000,000 times each block of code...

Results of isiterable: 0.381
Results of hasiter   : 0.777
Results of tryiter   : 5.689

The best result is isiterable while tryiter having the worst result.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isiterable-1.0.post1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

isiterable-1.0.post1-cp310-cp310-linux_armv7l.whl (9.8 kB view details)

Uploaded CPython 3.10

File details

Details for the file isiterable-1.0.post1.tar.gz.

File metadata

  • Download URL: isiterable-1.0.post1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for isiterable-1.0.post1.tar.gz
Algorithm Hash digest
SHA256 6610d298cd73fdf029ea4a692f64fc7ec241682d12b6bd9096776e94b87d6514
MD5 438d19bf515754c7a5381a56302e1bfb
BLAKE2b-256 8ac9fe8133fd8c93e00e0fc6d67e59abdb2434a47fb7a36305595859c60994fd

See more details on using hashes here.

File details

Details for the file isiterable-1.0.post1-cp310-cp310-linux_armv7l.whl.

File metadata

  • Download URL: isiterable-1.0.post1-cp310-cp310-linux_armv7l.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: CPython 3.10
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for isiterable-1.0.post1-cp310-cp310-linux_armv7l.whl
Algorithm Hash digest
SHA256 a49e723e546f2ea65bea27757d7e38e28814f75fc8a9d660ca779a9db4176e24
MD5 bded90e1db8f5ef7bf2bf7885564b11a
BLAKE2b-256 f2422a5bb640f575b1d6b802067565c93924862df023a0330e98482cc6065167

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page