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 *
or

Already have an account? Sign In »

Time
2 hours 30 minutes
Difficulty
Intermediate
CEU/CPE
2
Video Transcription
00:00
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
00:19
Intermediate Python. Excuse me.
00:22
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.
00:33
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.
00:49
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,
01:07
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,
01:14
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
01:32
could theoretically notionally be used as modules.
01:36
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
01:55
is a lot like a class. And there's a very simple reason for that, Which is that, like everything else,
02:00
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
02:20
packages have six subdirectories of other packages.
02:23
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
02:42
with an empty innit file. That file is just there to indicate to the Python interpreter
02:46
that this directory is in fact, a python package.
02:51
So the import process, how pipe on actually brings in files? Remember that in module, uh,
02:57
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
03:12
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.
03:19
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
03:38
and the bass finder the first finer that will activate
03:39
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
03:54
extensible classes. You can roll your own finder that can look in special places or use special logic to find and load modules.
04:02
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
04:16
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
04:25
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.
04:33
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.
04:49
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
04:58
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,
05:15
you may have noticed some of my code actually imports your code to run it.
05:19
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
05:34
and then doing your math.
05:38
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
05:56
and note that on Lee these above have the double underscore,
05:59
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
06:08
math, that pie.
06:10
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?
06:19
Aah! Help against that function and you can see that it returns X to the power of y.
06:28
So that's the basic import process for math. But what if we wanted to? And I'm gonna exit this real fast.
06:34
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
06:42
math dot pie
06:47
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.
06:59
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.
07:15
Additionally, something that you can do that is not recommended but is possible and functional
07:21
three
07:24
is to import everything from
07:27
from, uh, import
07:30
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
07:45
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.
08:01
But it does work, and you can see again that it imports everything without that math being prevented to it.
08:07
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
08:24
will continue to be driven home, which is that everything in python
08:28
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
Assessment
30m