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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -