[Tutor] modified dictionary class

John washakie at gmail.com
Mon Dec 6 13:34:37 CET 2010


Hello all,

I have been using this class extensively in my modules / scripts. It
provides the ability to .reference dictionary values. I find it handy,
but am afraid it may come back to haunt me at some point. Is there
anything wrong with using this?


class Structure(dict):
    """ A 'fancy' dictionary. It provides 'MatLab' structure-like
    referencing. Could also just define a generic class.
    Caution: This may be deprecated in a future release.
    """
    def __getattr__(self, attr):
        # Fake a __getstate__ method that resturns None
        if attr == "__getstate__":
            return lambda: None
        return self[attr]
    def __setattr__(self, attr, value):
        self[attr] = value

    def set_with_dict(self,D):
        """ set attributes with a dict """
        for k in D.keys():
            self.__setattr__(k,D[k])


More information about the Tutor mailing list