What's the meaning of underscores (_ & __) in Python variable names?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there this is Dan and this week I'm gonna answer a question that I got from Rio so Rio had a question around the meaning of underscore characters in Python variable names so I've put together this little example class or you know just a bit of example code that we can play with and here in the constructor of this class I'm creating three instance variables so I'm creating the Foo variable here assigning it a value I'm creating another variable called underscore bar and then also I'm creating a third variable called double underscore or dunder bass because often what we do when we talk about double underscores in Python we just shorten that to dunder so and so so in this case this variable here will be called dunder bass alright so what I'm gonna do now is I'm gonna create an object based on this class look at my little T object here which is a test object so when it comes to variable names the underscore and python has a specific meaning so the single underscore and a double underscore they both have meanings in Python and some of that is just by convention so what kind of everyone believes what an underscore should mean and some of it is actually enforced by the Python interpreter with the single underscore most of it is by convention so this is generally seen as a hint that a name is to be treated as private by the programmer so if this isn't really anything that's enforced by Python right Python doesn't have these strong distinctions between private and public variables like for example Java does but it's more a hint to another programmer that the variable or a variable that starts with a single underscore carries some internal state and is not really meant as part of the public interface of a class so it's best to leave it alone now with the double underscore or dunder prefix there is actually something that the Python interpreter does there's some name mangling it applies so it's going to change the name of that variable in a way that's going to make it harder to create collision when someone else extends this class and I know this sounds completely abstract so this is why I put this code example together here because what we can do now we can take a look at the attributes on this object right so now we're looking at the attributes in this object here and what we can do now is we can look for foo underscore bar and dunder bass in this list and then we're gonna notice some interesting changes so with the Foo variable that just appears here one two one right so self dot foo appears as foo on the class then we've got self underscore bar and that'll appear as underscore bar so you know nothing changed here is just a convention a hint meant for the programmer to say that this is a private variable or you know one that you should careful should be careful about changing now with the dunder Baz attribute here things are a little bit different because when you search for it here netlist there is actually no variable called dunder bass but there's one that is called underscore test which is the name of the class dunder bass and this is the name mangling that's going on so the Python interpreter does this to protect subclasses so for example if you were to extend the test class with another class that inherits from test it does that name mangling to prevent naming collisions because when you create another class based on this test class maybe you want to use the same name right maybe you want to use a similar variable name and then these two names would collide and it would kind of create a pretty horrible bug or the kind of crappy situation there so that is why Python applies this name mangling and of course this name mangling doesn't just apply to what you can see with the dur function but it also applies to well you're just accessing attributes on the object right so I can access T dot foo and T underscore a bar but if I go T and dot dunder Bath's the object doesn't actually have an attribute called dunder bass and this is exactly what happened there with the name mangling right so I can get at that attribute using the mangled name but this is a really bad idea right like don't do this so this is not a good idea because every time the name of the class changes the name of the attribute is gonna change and then you're gonna have a bad time really the only way to access these quote-unquote private attributes because they're not really private as you can see right there just mangled to avoid naming collisions really the only reasonable way to get at them is to access them from the class itself because here in the class you know every time you go self dot dunder Baz pythons can apply that mangling automatically and you don't have to worry about it it's more protection from you know subclasses changing the attribute or other code reaching into an object and trying to change it but you know as you can see it's a sort of a weak protection it's not really enforced in any way it's more you know through that naming change makes a little bit harder to actually accidentally break things all right so this is kind of the short and sweet explanation of what's going on with these variable names in the single underscore and double underscore prefixes that you sometimes see in Python code if you enjoyed this tutorial and you'd like to see more just like it then subscribe to my youtube channel I put out at least one new tutorial a week so just hit the subscribe button and you're gonna learn Python as we go along cool so talk to you soon and happy Python II
Info
Channel: Real Python
Views: 161,836
Rating: 4.9120417 out of 5
Keywords: dbader, python, learn python, python tricks, dunder, underscores, python variables, pythonic, pytricks, name mangling in python, python single underscore and double underscore, python tutorial, python programming
Id: ALZmCy2u0jQ
Channel Id: undefined
Length: 6min 0sec (360 seconds)
Published: Tue Apr 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.