Factory for Struct-like classes

eliben eliben at gmail.com
Wed Aug 13 13:46:45 EDT 2008


On Aug 13, 7:30 pm, Christian Heimes <li... at cheimes.de> wrote:
> eliben wrote:
> > Ruby's 'Scruct' class (http://ruby-doc.org/core/classes/Struct.html)
> > does this. I suppose it can be done with 'exec', but is there a more
> > Pythonic way ?
>
> Try named tuplehttp://code.activestate.com/recipes/500261/
>
> A named tuple implementation is part of Python 2.6 and 3.0. For older
> versions of Python use the recipe from activestate.
>
> Christian

Thanks Christian - this is exactly what I've been looking for.
Some thoughts...

1) I see this is done with exec anyway, so there's no more pythonic
way.

2) The definition of fields as a single string is weird. Why not use
**kwargs instead ?



More information about the Python-list mailing list