4.1 Module Deep-Dive

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

2 hours 30 minutes
Video Transcription
Hello, everyone. And welcome back to Intermediate Python with me. Your instructor, Joe Perry. I'm very excited to tell you that if you were watching this video, you have almost reached the light at the end of the tunnel. This is going to be the very first lesson of module three. The module module on this is going to be the last module of enemy
Intermediate Python. Excuse me.
So our first less we're gonna jump right into it is the module deep dive. We've talked about modules a lot. We've addressed them, allowing discussed them a lot. Now we're gonna actually understand.
So I have three core objectives. We're gonna define module. We're gonna understand exactly what a module really is in Python. And I have to warn you, it's slightly anti climactic. Then we're gonna discuss the import process, and then we're gonna jump back in or B m and actually examine a module in a little bit of detail.
So what is a module? The short and honest answer to that question is that any Python file can be a python module. A module is just a python, a chunk of python code or of any kind of code really that it can be imported into python. In fact, in the advanced program here in the advanced Python class,
we'll talk about how you can have see files. That function is modules, a completely different programming language. You've been still implement as a pylon module,
but for now understand that any pipe, any funk, any file that the Python interpreter can handle can be a module. Modules can contain variables, functions or actual code that runs if that module is invoked directly. And that's how it works. With most of the programs have created so far, all the Python script had created
could theoretically notionally be used as modules.
However, whenever you invoke them, remember that we have code that was automatically running, which means that whenever you invoked those modules that coat run. So one of the things that we're gonna discuss how to do in our next lesson, how to create code that only runs when that module is called as its own program. So those are the three main contents of module, and you'll recognize that variables and functions being contained in that
is a lot like a class. And there's a very simple reason for that, Which is that, like everything else,
modules or secretly classes, a pipe on file is just especially implemented class. Additionally, when I talk about modules, there's another subject that really needs to be understood. The concept of packages and a python package is really a directory of python files. It could be, you know, it could be a single file. It could be dozens of files. It could be messed in directories you have
packages have six subdirectories of other packages.
But all that really makes a python package is that it's a directory of python files, and it has this special file in it, not pie. And it's worth noting that, yes, that is exactly the in it that you see for a python class. What's fascinating is you actually don't have to put any code into that. You can use the package just fine
with an empty innit file. That file is just there to indicate to the Python interpreter
that this directory is in fact, a python package.
So the import process, how pipe on actually brings in files? Remember that in module, uh,
two, I think WeII spent some time discussing the OS module and we talked about we imported this module. We talked about its name space. We use some functions from it. What you can do with import. What import actually does is you just give it
the name of the module you want to invoke. In that case, we used OS on some case, you might use math, which is a model we're gonna examine here in just a second.
You might use sequel light. You might use socket. Are there An incredible number of python modules is one of the main strengths of the language. Really, that there are so many modules, so many custom libraries that people already written for U. S. O. When you search for a module, what happens is the python. The python interpreter has water called finders
and the bass finder the first finer that will activate
looks for built in models modules that come installed with your python. Instance. If that can't find the module that you're trying to import that it's going to report back and exceptions, gonna report back a failure and python will invoke its other finders. Now, finders are
extensible classes. You can roll your own finder that can look in special places or use special logic to find and load modules.
But generally speaking, you're going to have just the built in the first find. It looks for building modules and then a second finder that's also built into the Python interpreter that looks through the import path, which is something that you can define yourself in and add two and extend
on. Of course, once it's found that module, what has identified it is then going to give that module in name and local space, and that's how you're able to actually address
the functions. End of the variables inside of that model is because it has a local name under which you can address in the same way that you would with a class.
It is worth noting just as a side note that we're not really gonna explore very deeply. But the import live is a pipe on module that actually governs the import process. So if for some reason you want to mess with the mechanisms by which your program is loading modules, important live is the way you could do that.
So let's actually look at a specific model. In this case, we're going to examine their in this case, we're going to examine
the python module math, and we're going to do that from our incurred. I know we haven't been in the interpreter in a little while. That's okay. We're going to first clear our screen and we're just gonna import math now. This statement is very common in Python. You've probably seen it. If you looked at somewhere Labs on our next tech platform,
you may have noticed some of my code actually imports your code to run it.
There are lots of import statements that are very useful like that. But in general, what happens with an import is it just uses that finder that we talked about a second ago, identifies the module you're looking for and then just loads it into your space. And we can identify that we can see that by just running important math
and then doing your math.
Remember way back when in the intro, the python course, we talked about the idea of name spaces in the idea. When you called your against a specific object, you can see all of the sub functional, but what we now know tickle methods. You can address within it. So from the math module, we import all of this stuff
and note that on Lee these above have the double underscore,
which means that all of these are intentionally exported functions or variables that you're capable of using. And so we can examine this, for example, weaken type
math, that pie.
And we could see that it gives us pi toe a pretty pathetic number of digits. We can see that math, Doc, How is a function we can even do?
Aah! Help against that function and you can see that it returns X to the power of y.
So that's the basic import process for math. But what if we wanted to? And I'm gonna exit this real fast.
What if we wanted to use the functions side of math? We didn't want to have to constantly do that. Same addressing style where he had to do
math dot pie
or math? Not power, things like that. What if we just wanted to call those items directly? Well, there is a format for that built into the import process, and that is gonna be from math imports.
And then whatever you're trying to get So instead of importing the entire math module, this will import specific functions or specific attributes from that module and make those directly useful. So, for example, of wanted to do from math and pork time, then we just do pie. You can see that works just fine.
Additionally, something that you can do that is not recommended but is possible and functional
is to import everything from
from, uh, import
wild card. And that asterisk indicates that you're going to report everything in that module now. That's not generally recommended. The main reason is not recommended is because if that if that particular module has functions, have named collisions
if the implement of their own ad function, for example, which we're gonna do our next video. But if they implemented something that clashes with a python keyword, this could break your program. Or if they implemented something that clashes with one of your own function names or one of your own class stage or one of your own attributes, this could potentially break your program.
But it does work, and you can see again that it imports everything without that math being prevented to it.
So that's that's all there is to this video. That's gonna be the end of the first lesson of module three. We now understand what modules are we kind of understand the import process. We discussed how modules are actually found imported. And then we actually examined a module and started to understand the idea that
will continue to be driven home, which is that everything in python
is a class. So thank you all for watching. I hope to see you back in our next video where we're gonna roll our own custom module and complete all of our learning for this course. Thank you for watching. You've been watching Intermediate Python here on Cyberia, on demand, and I, as always, have been your host, Joe Perry. I look forward to having him back in our next video.
Up Next
Course Assessment - Intermediate Python