python - isinstance(x, list) when iterating a list containing strings and lists -
at iterating nested list inside-out, told "type-checking isn't pythonic". generally, true: want @ interface (duck-typing) rather specific type.
the question asks nested lists of form ['a', ['c', ['e'], 'd'], 'b']
, , consider strings atomic (non-iterable). so, can't utilize blanket collections.iterable
, on other hand isinstance(x, list)
seem bit hacky.
my reply was
def traverse(l): x in l: if isinstance(x, list): traverse(x) callback(l)
what's improve approach? or isinstance
ok here?
i think reply ok here -- although might alter
if not isinstance(x,basestring): ...
to create little more accepting depending on expected input. ultimately, problems need isinstance
why still exists in language.
that beingness said, thing problem un-pythonic data-structure. in python, if problem seems hard, means you're storing info in wrong way ... (of course, realize had no command on original poster of other question's info construction ;-).
basically, guess view isinstance
hack need create lemonade when colleagues/some library author somewhere gives lemons -- otherwise avoid possible.
python collections iterable
No comments:
Post a Comment