Skip to content

Commit

Permalink
ENH: avoid a try/except structure in yt.funcs.is_sequence. Update doc…
Browse files Browse the repository at this point in the history
…string to discourage usage
  • Loading branch information
neutrinoceros committed Sep 5, 2021
1 parent bff9354 commit 76a2bd4
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions yt/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import urllib.parse
import urllib.request
import warnings
from collections.abc import Sized
from functools import lru_cache, wraps
from numbers import Number as numeric_type
from typing import Any, Callable, Type
Expand All @@ -36,19 +37,15 @@
# Some functions for handling sequences and other types


def is_sequence(obj):
def is_sequence(obj) -> bool:
"""
Grabbed from Python Cookbook / matplotlib.cbook. Returns true/false for
Parameters
----------
obj : iterable
Check wether `obj` implements the __len__ protocol
Implementation used to be more complex. This function is kept for backwards compatiblity,
but it shouldn't be used in new code because the name conflicts with the standard library's
definition of a "Sequence".
See https://docs.python.org/3/library/collections.abc.html
"""
try:
len(obj)
return True
except TypeError:
return False
return isinstance(obj, Sized)


def iter_fields(field_or_fields):
Expand Down

0 comments on commit 76a2bd4

Please sign in to comment.