Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Miscellaneous Built-Ins

00:00 In the previous lesson, I demonstrated how to execute Python strings containing code in case you’re feeling very brave. In this lesson, I’ll cover the remaining built-in functions that I haven’t touched upon yet.

00:12 There are five functions I couldn’t quite group in with the others. They are help(), hash(), __import__(), memoryview(), and breakpoint().

00:21 One last time, enter the REPL to tackle the miscellaneous leavings.

00:26 Python lets you get at documentation through the built-in help system. If you call the function on its own in a REPL, it gives you an interactive session. That’s quite the screenful.

00:37 There are instructions here on how to use the help system and suggested topics to try out if you’re new to Python. At the prompt, you can type in something that you want more information on.

00:47 I’m going to look at the built-in str function,

00:51 which you’ll recall isn’t a function but a class, which converts any argument passed to the constructor into a string instance. The help screen has info about the class along with a short description of every method.

01:03 There’s a lot here, so I’ll let you check it out yourself. I’m just going to hit Q once to get out of here and Q again to get out of help mode. You can also skip the interactive thing and pass your query in as an argument,

01:17 which gets you to the same place as before. Note that I called help() passing in a string.

01:24 Some functions and methods can be passed in directly, as in their references, but not all of them, so it’s actually easier just to put in a string representation of the thing you’re looking up.

01:35 A hash function is one that takes a value and maps it to a number. It can act as a digital fingerprint. Passing the same thing to a hash function multiple times results in the same value returned, allowing you to use it as a proxy that identifies the object.

01:49 You have to be a bit careful with this as a hash isn’t guaranteed to be unique, and when two different things hash to the same value, this is called a hash collision.

01:59 Hash values get used to check data integrity, do security, and in fact often form the basis of how dictionaries work. When you stick an object in a dictionary in Python, it is using the object’s hash code under the covers to index it, which is part of the trick behind how recalling the object is so fast.

02:17 The built-in hash() function returns the hash code for an object.

02:24 This is the hash value for the string capital H, "Hello".

02:29 And of course, if I call it again, I get back the same hash value.

02:34 The lowercase string is a different object though, so it results in a different hash number. This works for more than just data types. A good percentage of objects in Python are hashable.

02:51 today() returns a datetime object with the current date. datetime objects are hashable. Not everything is though.

03:01 Lists and other mutable data types aren’t hashable, and that is why you can’t use them as dictionary keys.

03:08 Python has a whole support system that gets used when you import a module. The __import__() built-in function is part of that underlying mechanic.

03:16 Generally speaking, you should only use it if you’re doing something particularly tricky. In fact, there are higher-level tools in the importlib module that you should prefer.

03:25 That said, my job today is to cover all the built-ins. So let’s take a peek. First, just so you can see it, this is what the dt module looks like if I evaluate it in the REPL.

03:38 __import__() takes at least one argument, the name of the thing to import. It then returns the module that has been loaded as an object. When I evaluate the result, you can see that it’s the math module.

03:52 And just to prove that it is the math module, I can square root something. There’s not much else to show you here without going crazy overboard. There are other optional arguments that allow you to control what’s in the global and local scope, the submodules to be imported from the module you’re importing, and a parameter to control how deep in a directory structure to go looking for a module.

04:15 As I said, there are better ways than __import__() if you have to dynamically load code. So start with importlib if that’s something you’re trying to do.

04:23 __import__() really is a last resort. Way long ago, seems like eons now, you saw the bytearray() built-in function. Byte arrays store data using something known as the buffer protocol.

04:36 This is a mechanism in Python where data is stored in a continuous memory block, and the protocol specifies how to get at the different parts of it. The memoryview() built-in function allows you to see the bytes in memory of any object that supports the buffer protocol.

04:51 You typically use it for debugging. To demonstrate memoryview(), I’m going to need some memory to examine.

05:02 A little bit of snaky deja vu. And there’s a refresher of what that looks like. Let me pass that into memoryview().

05:12 Wow, that’s like a whole callback. Good times, good times. list() trick to the rescue.

05:21 And those are the byte values from what is stored inside of the byte array. Just to help make sense of that list, running ord() on capital H, the first letter in hiss, gives you ASCII code 72, which logically enough is the first byte in the byte array shown above.

05:39 A breakpoint is something you set to invoke a debugger at a place in your code. Historically, each library or tool you used had its own way of setting breakpoints.

05:48 That is until Python 3.7. That’s where the breakpoint() built-in function got added. Let me write a little function to show you how it works.

06:07 Now, when I run this, the debugger gets invoked when the breakpoint line gets hit. Python ships with a debugger named pdb. This isn’t my personal preference.

06:19 I normally use pudb, which has a TUI interface, but I figured I’d show you the default. I am currently stopped at our breakpoint. If this was loaded from a file, you’d be able to query the code, but because this was directly typed into the REPL, you can’t see as much.

06:35 You can use the args command to pdb to see the current function’s arguments.

06:41 There’s the 3 and 4 that I’m adding together. Now, to get things going again, I use continue. Now I’m back out in the REPL.

06:51 That 7 is the result from the call. If you’re using pdb, then the call to breakpoint() is the equivalent of import pdb, then pdb.set_trace(). So it saves a little typing.

07:03 If you’re using something else, like I am, you can set the PYTHONBREAKPOINT environment variable to contain the command that you want to invoke when a breakpoint gets hit.

07:13 If you’re using something like Visual Studio, then you may have to do a little bit of extra configuration to get all this to work, but it can be done.

07:22 For more on the hash function, learn how to build your own dictionary-like storage container, a hash table. There’s also a course and tutorial on advanced importing techniques if you’d like to learn how to dynamically import some code.

07:35 Or if you’d like to get all hardcore with a command-line debugger, this content will help you learn pdb.

07:42 Well, it’s been quite the journey. Last up, I’ll summarize the course.

Become a Member to join the conversation.