[Tutor] modified dictionary class

Joel Goldstick joel.goldstick at gmail.com
Mon Dec 6 14:06:23 CET 2010


On Mon, Dec 6, 2010 at 7:34 AM, John <washakie at gmail.com> wrote:

> 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
>

why not "return 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])
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101206/7c36310c/attachment.html>


More information about the Tutor mailing list