Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Mangling Member Names

Tutorial with more detail about classes, inheritance, and name mangling: https://realpython.com/python-classes/

00:00 In this lesson, you will learn about name mangling in Python. Name mangling is the process where Python automatically renames certain class attributes and methods, but not all of them, just those that start with two leading underscores.

00:15 So in the previous lessons, we’ve talked about the single leading underscore. This lesson is about two leading underscores in classes. The purpose is to prevent name clashes in class inheritance.

00:28 Now the topic of name mangling is much, much bigger than we can cover in the lesson today, so I’ll provide you with a link to a Real Python tutorial that explains this in quite a bit of detail.

00:40 For today, the best thing to do is for you to write your own little example. So let’s study the following. If you could please switch to your REPL or your terminal if your REPL isn’t active yet. In your terminal, type python or python3, that opens up the REPL and you’ll create the following class, class called SampleClass.

01:05 We need four spaces, 1, 2, 3, 4 to create the .__init__() methods,

01:12 which takes one attribute.

01:16 Then we need eight spaces, 1, 2, 3, 4, 1, 2, 3, 4, and now type self. and then not one but two underscores ao __attribute equals attribute.

01:31 Now let’s also create a method. So 1, 2, 3, 4, and two leading underscores, __method.

01:44 And then eight, 1, 2, 3, 4, 1, 2, 3, 4. And just type a print() function here so self.__attribute. So all the method does is it will print the attribute value or the value that is captured by self.__attribute.

02:03 Okay, so that is our class. Press Enter, and then press Enter again so you see the three arrows. And let’s test this. So let’s create a sample_instance from the SampleClass

02:16 and the attribute will just make that string that says “Hello!”

02:23 So now we have our instance, let’s check the variables that are in this instance. So use vars().

02:33 You get a dictionary that holds the variables and you can see that there is a variable called _SampleClass __attribute. And that is the “Hello!” that we entered earlier.

02:42 But look at what happened here. _SampleClass, there’s no dot anymore. It doesn’t say SampleClass.__attribute. This has all become one name.

02:52 So Python has taken the double underscore and then attribute and just put it together with the name SampleClass and then added an underscore in front of that.

03:04 So that is the mangling. The consequence of this, though, is that the attributes and the methods are no longer available in their original format if you like, or by their original names.

03:15 So if I go to sample_instance

03:19 and I would like to call __attribute, so .__attribute, that gives me an error message. Now, despite the fact that my class actually has this attribute, because here it is self.__attribute, that is no longer available because Python has renamed that to _SampleClass__attribute without the dot.

03:43 So it has mangled the class name and the attribute name together and the same thing applies for the method. So we have a method __method, let’s see if we can access that.

03:55 So sample_instance.__method,

04:01 and I get the same error message that that SampleClass object has no attribute __method. Let’s clear the screen with Ctrl + L and see what dir() can give us.

04:14 Okay, so dir() of the sample_instance.

04:21 So dir() shows us that we have the two mangled objects here. You have the mangled attributes and the mangled methods. So they are one word, there’s no dot notation.

04:32 What I wanted to show you is that these methods or these objects are available outside of the class using the mangled name. Let’s give that a go. So sample_instance

04:46 and using the dot notation, I should have access to the mangled method, for example, or the mangled attributes. So let’s try the method first. That is _SampleClass

04:58 and then __method. And then don’t forget the brackets and indeed, “Hello!” gets printed, which is what our method is doing. And just to finish this off, also going to try the attribute _SampleClass __attribute.

05:21 So using the mangled name, and indeed the string “Hello!” is shown on screen. So in summary, you’ve learned that within the class the internal methods can be called using their original name.

05:36 Remember that the methods __method calls .self .__attribute. So that is the original name and that happens within the class, but that original name is not available outside of the class.

05:52 Now, even though __attribute and __method are internal attributes to the SampleClass class, they are available though outside of the class, but you need to use the mangled name.

06:06 In summary, name mangling is a process where Python automatically renames class attributes and methods that start with two leading underscores. We had __attribute and __method.

06:20 So __attribute was renamed automatically in the background without you having to do anything to _SampleClass__attribute so in one word, and the same thing for the method.

06:33 The consequence, as we saw, was that you no longer have access to the attribute or the method from outside of the class using their original name. The purpose is to prevent name clashes in class inheritance, and there is a detailed Real Python tutorial that explains everything you need to know about class inheritance.

06:53 So now that you’ve had an introduction to name mangling as a consequence of using two leading underscores in classes, let’s move on to dunder methods or magic methods that have two leading and two trailing underscores.

07:08 That’s for the next lesson. See you there.

Become a Member to join the conversation.