0

In a class like this:

    class F(object):
        def __init__(self, x=0):
             self.__x = x

        def X(self):
            return self.__getattribute___('__x')

Is it possible to use __getattribute__ for getting private attributes? Doing such Python 3 says F has no attribute '__x'. If my class is like this instead:

      class F(object):
          def __init__(self, x=0):
               self.x = x

          @property
          def x(self):
              return self.__x

          def X(self):
              return self.__getattribute__('x')

__getattribute__ does work. I know private attributes aren't accessible outside of the class; does the first case not work because of something related to that?

Daniel
  • 127
  • 1
  • 9
  • Try `'_F__x'` - using leading double underscores invokes name mangling, don't use it for *"privacy"*. – jonrsharpe Oct 26 '16 at 20:59
  • Why are you using `__getattribute__` anyway instead of `getattr`? – BrenBarn Oct 26 '16 at 21:00
  • 1
    Or, for that matter, just `return self.__x`, which *would* be unmangled correctly. I'd recommend reading http://stackoverflow.com/q/7456807/3001761. – jonrsharpe Oct 26 '16 at 21:01

0 Answers0