Check out a free preview of the full Practical Guide to Python course

The "Helpful REPL Methods" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina shares three helpful methods that can be used in the REPL. The type() method will return the type of a variable. The dir() method returns all the built-in methods for a class. The help() method returns the documentation for any class type or class method.

Preview
Close

Transcript from the "Helpful REPL Methods" Lesson

[00:00:00]
>> There is a special value in Python for none or no meaning, no value, and that has the special keyword none. If I type it in the repo, I don't get any output, which when you think about it kinda makes sense, right? If I tried to use a variable that I have not defined in my session at all, I will get a name error that says, name goodbye is not defined.

[00:00:34]
But if I want to have a placeholder variable, I can go ahead and say, goodbye is equal the keyword none with a capital N. And then it's defined within the scope of my program even though it doesn't have a value. Now, there are three helpful methods in the repo that you're gonna end up using all the time.

[00:00:59]
So if you have a posted note handy for things that you wanna take away from this class, I recommend writing these three down, I use them all the time, day in, day out. And these are. We have help(), type(), and dir(). Now the first one is going to tell us what the type of a variable is.

[00:01:33]
So if I have a number here, I can pass that into the type built-in method, and it will return class int, I can also pass in a value as well. So if you're not sure about what the type of something is, either while you're in the repo or as you're executing code, you can figure it out this way.

[00:02:05]
Let's try this with a string. What will I see when I get back when I ask for the type of name? Which built-in class?
>> Str.
>> Str, yes, that's exactly right. Now that we know the objects type, we can move on to other methods. Dir stands for directory, which means show me the directory of all the methods that are available for this type.

[00:02:41]
And I want to call dir on the type, not the variable name or the value of the variable, but on the type itself. So in this case, I can ask for the dir of str, because remember, if I type str my repo here, I'll see cluster, this is a built-in type, I don't have to define it.

[00:03:01]
So when I type dir of str, I will see all of the methods that are available on strings. You can for the most part ignore all of these that have the double underscores, those are internal methods that aren't meant to be used. And if you take a look at the other ones, you'll see some really useful ones like is alpha, meaning does this string only contain characters and not numbers.

[00:03:30]
There's a split method for splitting a string on tokens, there's a title method for title casing something. So that's how you can kind of very quickly see what is available on the type that I'm working with. Now, the last one is super awesome, because it lets you see documentation for Python right in your repo without having to open a browser, figure out what version you're looking for, navigate and search, and that is help.

[00:04:06]
So with help, you also want to pass in a class. So if I type help for str, it's gonna open this up for me, this interface. I can press space to scroll down, or I can press Q to quit. And that is for Linux and Mac systems, it might be something different in Windows.

[00:04:32]
So this is a lot of stuff, this isn't very helpful, instead I find it helpful to call help on a particular method. And I can do that by saying dot and then picking any one of these. And that just prints out the help for that specific method, and it also shows me what arguments this method expects.

[00:04:59]
So is alpha returns true, if the string is an alphabetic string false. Otherwise, if you're ever not sure, like I don't remember the name of that method, or I don't remember what this does, or I don't remember what arguments this accepts, you can always reach for help. And I believe that you can call help on values as well, maybe not.

[00:05:19]
Let's try this out. Yep, so dir is, sorry. That is not what I wanted. I have a file named hi, yeah. So for help you also want to make sure that you're passing in the class and not like a variable name or a value. Make sure that you remember these three commands, and you will not find yourself too lost.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now