[Python-ideas] Syntax for making stuct / record / namedtuples

Eero Nevalainen eero.nevalainen at indagon.com
Wed Oct 21 11:13:22 CEST 2009


 From the (creative) laziness department:

Whenever my module A needs to pass a lot of data to module B, I find 
myself making a factory function in module B for creating the datastructures

def my_factory_function(a, b, c):
     # input checking
     res.a = a
     res.b = b
     res.c = c
     return res

I believe this is fairly common.

Since the fields are already defined in the function signature, I'd 
prefer to not repeat myself and write something like this:

def my_factory_function(a, b, c):
     args = locals()
     # input checking
     return namedtuple('MyTypeName', args)


This would perceivably be possible, if locals() returned an OrderedDict 
and an appropriate namedtuple factory function was added.

related discussion is in:
http://kbyanc.blogspot.com/2007/07/python-aggregating-function-arguments.html
http://code.activestate.com/recipes/500261/

Does this seem familiar or useful to anyone besides me?

-- 
Eero Nevalainen



More information about the Python-ideas mailing list