I figured I'd start a series of Python challenges in my blog. Whenever I've posted questions such as these in the past, the discussion that follows is always very entertaining!
A discussion arose today at work about the best way to flatten a tuple consisting of values and other tuples (which may be nested arbitrarily). So the tuple (1, (1, 2, (1, 2, 3), 3)) would become (1, 1, 2, 1, 2, 3, 3). Now there was actually code implemented which was functional, efficient and easy to read – but where is the fun in that?
I figured I could re-write it in a more terse manner, and here is what I came up with:
def flatten(tpl): return eval(repr(tpl).replace('(', '').replace(')', '')) t= (1, (1, 2, (1, 2, 3), 3)) print flatten(t)
This flatten function would actually work in the context of our app, but I would never use it in production code. My challenge today, is to tell me why this code should never be used!
11 Responses to "Nesting Instinct"
You could have string values (for example) with parenthesis in them. These values would be changed.
A further reason is that you have no idea what the repr of the object looks like, since you don't even examine its type …
One must hope nobody starts devising malicious objects with exploitive repr()s.
Cute idea, though.
I can't believe you had the audacity to post that! Very amusing though. Let me know when you can properly do it in 2 lines
How about this, Euan…
Not quite as concise as the the other flatten, but less of a crime against Python!
2euan: is this “properly”?
A safe variant of your solution, assuming the values are integers:
def flatten(tpl):
return tuple( map(int, repr(tpl).replace('(', ‘').replace(’)', ‘').split(’, ')) )
Posting again, sorry:
oops, didn't see Will's, which is cleaner.
Since I've been glancing at the Y combinator [paddy3118.blogspot.com]
Remember: Don't do this at home folks :-)
- Paddy.
One liner variation, working with any iterable:
flatten = lambda t: sum(map(flatten, t), ()) if hasattr(t, ‘__iter__’) else (t,)
I recommend a visit to http://www.challenge-you.com [challenge-you.com] if you enjoy those kind of challenges.