Skip to content

Instantly share code, notes, and snippets.

@jss367
Last active July 4, 2020 15:50
Show Gist options
  • Save jss367/d63f446a0db19d17f345810b579ffe6b to your computer and use it in GitHub Desktop.
Save jss367/d63f446a0db19d17f345810b579ffe6b to your computer and use it in GitHub Desktop.
# Sometimes I make functions that can either allow an object or a list of objects.
# To do this I check if an input is an object and, if so, wrap it in a list.
# Here's a simple way to do that.
def _isArrayLike(obj):
return hasattr(obj, '__iter__') and hasattr(obj, '__len__')
objs = objs if _isArrayLike(objs) else [objs]
#Another option is to do the opposite check:
if isinstance(obj, ObjType):
obj = [obj]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment