python - Use a metaclass's __getattribute__ method to modify the returned attributes -
i'm trying implement* class/type use enumerator keys dicts. want modify attribute values returned, example, custom class:
class testkeys(keys): 1 = 1 2 = 2 instead of having testkeys.one return 1, want override return like, (the string) "testkeys.one". i've tried doing following, falls infinite recursion...
class _keys_meta(type): # store attribute values list ``__values__`` # (if dont start '__') def __init__(self, name, bases, dict): self.__init__(self) self.__values__ = [ self.__dict__.values()[ii] ii,ent in enumerate(self.__dict__) if not ent.startswith('__') ] def __getattribute__(self, attr): if( not hasattr(self, '__values__') ): self.__values__ = [] if( attr in self.__values__ ): return "{:s}.{:s}".format(self.__name__, str(attr)) return object.__getattribute__(self, attr) class keys(object): __metaclass__ = _keys_meta how can modify values returned when attribute accessed?
*i familiar enum34, don't of features.
Comments
Post a Comment