1

I'm trying to understand about private variable and about name mangling in python.

When I defined class like this, it throws AttributeError

>>> class E():
    def __test():
        print("__test()")
>>> c = E()
>>> c.__test()
AttributeError: 'E' object has no attribute '__test'

I want to understand the exact reasons, for this error. Can any one explain them clearly?

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
Python coder
  • 743
  • 5
  • 18
  • 3
    This is like 3 or 4 different questions rolled into one... – Aran-Fey Sep 02 '19 at 17:05
  • have you found very similar questions like https://stackoverflow.com/q/2709821/1358308... what can we add here that hasn't been said before? – Sam Mason Sep 02 '19 at 17:08
  • @Aran-Fey Sorry, for that. As those are inter linked with each other, asked in a single question. Can you please help me to understand them clearly? – Python coder Sep 02 '19 at 17:10
  • As for the `self` vs. `something`: the name doesn't matter, as long as you understand that the first argument will be the object that invoked the method. – chepner Sep 02 '19 at 17:10
  • Name mangling is a rather advanced feature, and rarely used in practice. You should concentrate on more basic things: for example, the first argument of your methods should be `self`... – Thierry Lathuille Sep 02 '19 at 17:11
  • To understand *exactly* what is going on, you need to understand the [descriptor protocol](https://docs.python.org/3/howto/descriptor.html). Once you understand that, you just need to know that the `function` type implements the descriptor protocol, with `function.__get__` returning a (bound) `method` value that wraps the original function. – chepner Sep 02 '19 at 17:13
  • But name mangling and how methods work are two entirely separate questions. – chepner Sep 02 '19 at 17:13
  • @Aran-Fey, question was edited to limit it to a specific problem. Can you please tell for whom should I ask to review and remove `hold` flag? – Python coder Sep 02 '19 at 17:49
  • 1
    There must be a duplicate that covers this. – chepner Sep 02 '19 at 17:59
  • @Pythoncoder The first edit after question closure automatically puts it into the reopen review queue. But chepner's right, it'll just be closed as duplicate. – Aran-Fey Sep 02 '19 at 18:08

1 Answers1

4

I'll refer you to Python's documentation on name mangling.

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.

So this raises an AttributeError because the __test methods has been implicitly renamed:

class E:
    @staticmethod
    def __test():
        print("foo")

E.__test() # AttributeError

But can be made to work by using the mangled name _E__test:

class E:
    @staticmethod
    def __test():
        print("foo")

E._E__test() # prints 'foo'

Notice that I had to add a staticmethod decorator due to your method not taking a self argument. This is out of scope, but you can read about it here, here and here.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73