Two aesthetically pleasing Python snippets

By Chris Lamb

The following Python tree structure recently resurfaced. I find it rare to see functional or recursive techniques expressed so succinctly in Python:

def tree():
    return collections.defaultdict(tree)

Another construction I am quite fond of is:

for _, dirnames, filenames in os.walk(path):
    break

...which leaves dirnames and filenames in the current scope, containing the immediate subdirectories and files of the specified path.

Perhaps a little too cute, but appealing in its own way.


Chris Lamb is a freelance software developer and the current Debian Project Leader. You can read other posts by me, see software I have written or read more about me. You can also follow me @lolamby.


Tags: Hacks

Planets: ALUG UWCS WUGLUG Debian

Tuesday 11th March 2014


Three comments