From 41079fd92ce611ff5fe40b73a554dd732e64c66a Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Mon, 19 Mar 2012 11:57:19 -0400 Subject: [PATCH] * sugar: Added is_iterable(). --- sugar.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sugar.py b/sugar.py index 75bf039..38a4010 100644 --- a/sugar.py +++ b/sugar.py @@ -13,6 +13,15 @@ import sys import weakref +def is_iterable(x): + """Checks if an object x is iterable. + It checks only for the presence of __iter__, which must exist for + container objects. + Note: we do not consider non-containers such as string and unicode as + `iterable'; this behavior is intended. + """ + return hasattr(x, "__iter__") + def ifelse(cond, trueval, *args): """An alternative to python's own ternary operator, but with multiple conditions to test (like chained if-else-if-else... which is found in