Check whether or not an object can be iterated over. Parameters ---------- y : object Input object. Returns ------- b : bool Return ``True`` if the object has an iterator method or is a sequence and ``False`` otherwise. Examples --------
(y)
| 347 | |
| 348 | @set_module('numpy') |
| 349 | def iterable(y): |
| 350 | """ |
| 351 | Check whether or not an object can be iterated over. |
| 352 | |
| 353 | Parameters |
| 354 | ---------- |
| 355 | y : object |
| 356 | Input object. |
| 357 | |
| 358 | Returns |
| 359 | ------- |
| 360 | b : bool |
| 361 | Return ``True`` if the object has an iterator method or is a |
| 362 | sequence and ``False`` otherwise. |
| 363 | |
| 364 | |
| 365 | Examples |
| 366 | -------- |
| 367 | >>> np.iterable([1, 2, 3]) |
| 368 | True |
| 369 | >>> np.iterable(2) |
| 370 | False |
| 371 | |
| 372 | Notes |
| 373 | ----- |
| 374 | In most cases, the results of ``np.iterable(obj)`` are consistent with |
| 375 | ``isinstance(obj, collections.abc.Iterable)``. One notable exception is |
| 376 | the treatment of 0-dimensional arrays:: |
| 377 | |
| 378 | >>> from collections.abc import Iterable |
| 379 | >>> a = np.array(1.0) # 0-dimensional numpy array |
| 380 | >>> isinstance(a, Iterable) |
| 381 | True |
| 382 | >>> np.iterable(a) |
| 383 | False |
| 384 | |
| 385 | """ |
| 386 | try: |
| 387 | iter(y) |
| 388 | except TypeError: |
| 389 | return False |
| 390 | return True |
| 391 | |
| 392 | |
| 393 | def _average_dispatcher(a, axis=None, weights=None, returned=None, *, |