class attrdict

goodwolf Robert.Katic at gmail.com
Sun Mar 4 13:06:07 EST 2007


class Namespace(object):
    def __init__(self, __ns=None, **kwargs):
        if __ns is None:            #if no dictionary is given
            self.__dict__ = kwargs  #then use kwargs without copying
or creating new dict
        else:
            assert len(kwargs) == 0
            self.__dict__ = __ns    #else use dictionary without
copyng

     #additional methods for JS like object (ONLY FOR DEMONSTRATION)
     def __getitem__(self, name):
        return getattr(self, name)
    def __setitem__(self, name, value):
        setattr(self, name, value)
    def __delitem__(self, name):
        delattr(self, name)
    def __iter__(self):
        return iter(self.__dict__)
    def __contains__(self, name):
        return hasattr(self, name)




More information about the Python-list mailing list