I’m trying to keep a dictionary of open files for splitting data into individual files. When I request a file from the dictionary I would like it to be opened if the key isn’t there. However, it doesn’t look like I can use a lambda as a default.
e.g.
files = {}
for row in data:
f = files.get(row.field1, lambda: open(row.field1, 'w'))
f.write('stuff...')
This doesn’t work because f is set to the function, rather than it’s result. setdefault using the syntax above doesn’t work either. Is there anything I can do besides this:
f = files.get(row.field1)
if not f:
f = files[row.field1] = open(row.field1, 'w')
This use case is too complex for a
defaultdict, which is why I don’t believe that something like this exists in the Python stdlib. You can however easily write a generic “extended”defaultdictyourself, which passes the missing key to the callback:Usage:
This works in Python 2.7+, don’t know about older versions 🙂 Also, don’t forget to close those files again: