is this data structure build-in or I'll have to write my own class?

Robert Bossy Robert.Bossy at jouy.inra.fr
Thu Feb 21 03:52:12 EST 2008


mkPyVS wrote:
> This isn't so optimal but I think accomplishes what you desire to some
> extent... I *think* there is some hidden gem in inheriting from dict
> or an mapping type that is cleaner than what I've shown below though.
>
> class dum_struct:
>    def __init__(self,keyList,valList):
>       self.__orderedKeys = keyList
>       self.__orderedValList = valList
>    def __getattr__(self,name):
>       return self.__orderedValList[self.__orderedKeys.index(name)]
>
>
> keys = ['foo','baz']
> vals = ['bar','bal']
>
> m = dum_struct(keys,vals)
>
> print m.foo
>   

Let's add:
    __getitem__(self, key):
        return self.__orderedValList[key]


in order to have: m.foo == m[0]

RB



More information about the Python-list mailing list