Exploring an Interface Example
00:00
In this video, you’ll be looking at a real-life example. You’ll be looking at the dir()
function. We’ll be using that function to investigate the string class or the str
class from the built-in functions in the Python standard library.
00:14
So please move to your REPL. So in your terminal, you start your REPL, as we have done before using python
or python 3
. You see the three arrows in front of your cursor so you are in the REPL, and you type in dir(
00:31
str)
, press Enter. And now you can see all the objects that are available in the string class, the str
class. So you have __add__
, __class__
, lots of those that have underscores.
00:47
One of those is __init__
. But then there’s also quite a few that don’t start with an underscore: capitalize()
, casefold()
, center()
, count()
, and so on.
01:01
So what have you learned from the dir()
example or the str
example? There were some public methods there: isnumeric()
, replace()
, split()
, so they don’t start with an underscore.
01:13
There were special methods there: __add__
, __init__
. That’s quite a tongue twister. So that’s double underscore add
, double underscore, or a dunder method.
01:25 And we will talk about those in more detail later in the course. But there were a few of those with two leading and two trailing underscores. Were there any non-public ones starting with a single underscore?
01:38 I didn’t see one, but let’s have a look at the output.
01:43
I don’t see any that starts with an underscore. I see format_map()
, which clearly has an underscore, but it doesn’t start with one.
01:54 So none of the non-public methods starting with a single underscore. And I also didn’t see any starting with a double underscore that did not end with two underscores.
02:05 So no non-public methods starting with a double underscore.
02:10 So now that you know the difference between a public and a non-public interface, you are ready to tackle single leading underscores. See you in the next lesson.
Become a Member to join the conversation.