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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Functions: Variables, References, and Scope (Part 1)

Give Feedback

00:00 Variables, References, and Scope.

00:05 Variables, References, and Scope: dir().

00:10 One of the most useful functions that I found during my time using Python is dir(), for directory. This allows you to see all of the attributes and methods that any Python object has. This may seem a rather abstract concept, but it allows you to understand what you can do with any object.

00:31 Let’s create an object and get started with it. So firstly, we’ll create a string. Here the string a is 'hello'.

00:40 And we may not know all of the methods that you can apply to it, so for instance, you might know a.upper() provides an uppercase version of that string, or you can use a.capitalize()

00:55 to capitalize the initial letter of the string.

00:59 You may have forgotten what methods are available to you, and if you’re not using something like bpython, they may not be pointed out to you. Now, while this may not give you the full documentation on the methods or attributes that are available, it can jog your memory or allow you to search for the right term.

01:16 So here, using dir(a) we can see we get all of these methods and attributes which are available for a string. Some of these may be immediately apparent, some of them won’t, but they’re all things that you can examine in more detail or do a targeted search, or experiment with them and see what happens.

01:36 It’s a really important part of learning to understand things in your own terms. Often, other people’s explanations may not really hit the mark, and if you’ve experimented with it and understand it in the way that you need to, then you’ll know what to do with an object and be able to use it most efficiently. So here, we’ve discovered the method a.center() and if we put in 50, we can see that it’s centered the hello string within a 50-character gap, which can be really useful when trying to produce headings et cetera, and it’s exactly the kind of thing that I replicated myself in code which worked out how long the text was and how long the spaces were, and it took me many lines to do what .center() could do in a single method.

02:18 This works for any kind of object in Python, so this is hugely useful and really important. No matter what kind of objects you’re dealing with, you can perform dir() on it and see what kind of methods and attributes it has.

02:32 Here we’re importing datetime and doing dir(datetime), and we can see that inside it, there are other submodules.

02:43 I happen to know that datetime.datetime has some useful things in there, so we can do dir(datetime.datetime), and in there there’s a list of functions and attributes which we can experiment with or search for to know exactly what we’re dealing with.

03:00 The slightly intimidating part of this is all of these what are often called dunder methods, with a double underscore (__), and these are generally part of the internal working or initialization of objects.

03:11 They’re things that you probably won’t need to worry about at this stage but as your programming develops you may need to make use of them later on and you’ll know that they’re there.

03:20 But for the time being, being able to find out what kind of things an object supports—or even if an object is what you think it is—can be really useful, and then you can look into experimenting with them to improve your understanding of what’s going on and what you can do with an object.

emalfiza on Jan. 25, 2020

@Darren Jones, yoy are such an amazing pythonist. I have never come across a pythonist tutor like you. Never heard Dir() anywhere from anyone hats off to you.

Jagadeesan on June 8, 2020

Concepts explained clearly. Thanks.

Become a Member to join the conversation.