Following Naming Conventions
00:00 When talking about naming conventions in Python, there are really two PEPs we need to talk about: PEP 8 and PEP 20. The PEP stands for Python Enhancement Proposal.
00:12 So a PEP is a design document that provides all sorts of information to the Python community. Let me guide you through the relevant section of PEP 20 first.
00:22 PEP 20 is the Zen of Python. These are the guiding principles for Python’s design. And to find out more, you can enter PEP 20 in your favorite search engine, but the Zen of Python actually has a cool little Easter egg that I would like to show you.
00:38 And for that we will need the REPL. So please go to your terminal,
00:43
and in your terminal type python
to start the REPL. If you are in Windows, type python3
if you are on mac and for Linux, it is either python
or python3
depending on your distro.
00:57
So for me, it’s python
. You then see the three arrows there in front of your cursor, that means you’re in the REPL and all you have to do to find out about the Zen of Python is type import this
.
01:11 What you will then get is 19 aphorisms about Python. The one I wanted to show you is the seventh one down that says readability counts. So readability is very important, and we will come back to that in a minute.
01:26
For reference, I’ve included this screenshot in your slide deck so you can come back to that later so you don’t forget about the import
this
Easter egg.
01:35 And the one that was important for us at the moment is readability counts.
01:41 You should, however, also consider PEP 8, which is the style guide for Python code and it describes Python coding conventions. So in this guide, you will find the following section: It reads “One of Guido’s key insights is that code is read much more often than it is written.
01:59 The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code.” Now Guido here is Guido Van Rossum.
02:11 He’s the founder of Python, so he created the Python programming language. But the key points here are readability and consistency. And if you keep reading PEP 8, you’ll also find a section called descriptive naming styles.
02:26 And on the following slide, you’ll find a summary of the naming conventions that are relevant for this course.
02:33
So this is a summary and these are the naming conventions we’ll be talking about. The first one, single leading underscore. For example, _validate
indicates that the name is meant for internal use.
02:46 And we’ll come back to what that means in the next lesson. There’s a double leading underscore attribute that triggers name mangling in Python classes. And again, we’ll come back to what that means.
02:59
There’s the double leading and trailing underscore, so __len__
as an example. That indicates a special magic attribute or a magic method that Python provides.
03:13
Then there’s a single trailing underscore list_
, for example, and that is used to avoid naming conflicts with Python keywords and built-in names.
03:24
And finally, we’ll look at the single underscore used as an _
, and that indicates a temporary or a throwaway variable. Later on in the course, you’ll see two more uses of the single underscore.
03:37 Firstly, in a REPL session, an underscore holds the value of the last evaluated expression. And secondly, since Python 3.10, the underscore is used as a wildcard in structural pattern matching.
03:52 What that means, you’ll cover that in more detail later in the course.
03:58 Conventions are optional. They are not rules, they are guidelines. But as you saw when you were exploring PEP 20, the Zen of Python and PEP 8, the Python style guide, conventions are created to improve readability and consistency.
04:12 Therefore, they are considered best practice. Sometimes, however, they are more than just best practice. Your code might actually crash if you don’t stick to conventions.
04:24 If you move to the REPL, we can work on a little example that shows you what I mean. In your terminal, you will find the REPL still open from the previous session.
04:34
If not, again type python
or python3
to open the REPL. And a little tip here, if you want to clean your screen, which I tend to do, I press Ctrl plus L and that cleans your screen.
04:47
So in our example, you will be creating a little class called Person
where the class only has one property or one attribute, which is name
.
04:58
So type class Person
with a capital that is a naming convention. And then hold on, you then see the three dots in the REPL. We need four spaces for indentation, so 1, 2, 3, 4.
05:12 Quick note here. If you are using Python 3.13, you won’t have to bother with calculating indentations. The REPL in Python 3.13 has been upgraded with a number of cool features.
05:24 Now I haven’t upgraded yet, so I am still counting.
05:34
(self, name)
:
, enter. Now we need eight indentations. 1, 2, 3, 4 to be at the def
level, and then 1, 2, 3, 4 to indent from def
.
05:48
And here you type self.name = name
.
05:56
method is called a dunder method and we will talk about those methods in a bit more detail later on in the course. But for now, please know that the __init__
method is needed to make your class actually work.
06:10 If you don’t know about classes and how they work exactly, I’ll include a link to a Real Python course that explain classes in a lot of detail. But for now, let’s just leave it at that and go, okay, we have our little class, press Enter.
06:25
You see the three dots, press Enter again. So you see the three arrows. And now let’s try to instantiate this class. So type me = Person
with a capital.
06:36 This is where you call the class and you have to give the person a name. So in my name or in my case, that is Steven. I’ll press Enter and nothing happens.
06:46 But importantly, as we will see later, there is also no error message here, so this means this class works properly. What I could do, for example, is ask what the name of this person is.
07:00
and you can see that the name of this person is Steven. So far so good. So we have a working class. What I would like to show you now is that if you deviate from the __init__
function where you have __init__
that the code will actually crash.
07:17
So we’re going to create the same class again, but instead of using __init__
, you’ll be leaving off the two trailing underscores. So let’s start again.
07:29
class Person
, press Enter, four spaces, 1, 2, 3, 4, def __init__
. And now let’s leave off the trailing underscores and let’s type self, name
.
07:44
Now we need again eight indentations 1, 2, 3, 4, 1, 2, 3, 4, and the same thing. self.name = name
. We have now created a class. Press Enter twice so that you get back to the three arrows.
07:58
And now let’s try to instantiate the class again. So me = Person
. And then as a name, I’m going to use Steven again
08:07
and hit Enter and you will get an error message saying Person
takes no arguments. The actual detail behind the error message is not important at the moment, but what I wanted to show you is that if you don’t stick to the convention of having two trailing underscores after __init__
that your class won’t work.
08:29 For reference, I include this example here in this slide deck. And so now that you know how important naming conventions are and when you should use them, you’ll learn about public interfaces and internal use in the next lesson. I’ll see you there.
Become a Member to join the conversation.