4.2 Creating a Module
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
Hello, everyone. And welcome back to Intermediate Python here on side Mary, on demand, I have always and your instructor, Joe Perry And I'm excited to tell you that this is gonna be the last actual lesson of this course.
The next lesson, Lesson three is going to be our course summary and review, Which means this is gonna be the last lesson in which we introduce new material. And of course, this is gonna be a lesson to of Module Three,
in which we will be creating a module
so we only have one learning objective. We only have one task in this final video. Using what we learned in our previous lesson about modules and how they work and how they're imported, we're going to create our own. We're gonna create a custom python module. We're gonna turn that into a python package.
So let's jump right in or vm and take care of business. So we're here. We're in our bm again, Terminator. Everything's pretty standard. The first thing we want to do is want to create our modules directory. So we're gonna do make a dirt my mob,
and then I'm gonna see the into it
first thing I remember that I talked about when we're creating a python package, the first thing, the one thing that you have to have to make it a proper python, that package is the pro is the code
file theory of the file in it dot pie
and we don't have to put anything in that We just needed to exist,
So we're just gonna touch in it dot pie.
Then we're going to create our actual python module that we're gonna be importing here in just a second and in this case, what we're gonna import or we're gonna create if we're gonna create them and we're gonna use them to create
and we're gonna give the shebang line just like usual. And mostly, this is gonna be a standard python program right here. All we're gonna do is we're gonna define that. We're gonna create a few custom functions. Gonna say death. My add a B
A plus B
death my sub A b
a minus B
deaf. My div a B
return aid by a B and f my mole maybe
a times B.
Now, when we read a normal python for when you write a pilot program that doesn't have any, you know, any module intense or any special intense built in this would pretty much be it, except for the code that actually invokes these functions. So we might do something like that. Mane,
Uh, my ad
print my AB
and then down here, the bottom we would just call May. Now, this is how we would run this program as their This is how we run this code as a normal python program. But I want to create a custom. Want to do something special with it? What we're gonna do here is, instead of doing may actually going to use a special python invocation. And that python invocation is if name
no. The double underscores
is equal to
Then we're going to call the main function. Now what? This Python indication? What this little trick does is that it actually examines the module itself the python program that this code is founded to determine whether or not it has been important. I talked about that part of the import processes the finder and the other part is loading
in the loading process for a python module,
it assigns a name. And generally speaking, that name is gonna be the name of the module. However, if a module is running on its own, if it's just directly in folks than it's going to be named Main. So it's a really useful python shorthand. You'll probably see in a lot of python programs that says, Hey, if this is being invoked by itself, run the main function
and we can see that here.
We can see that here just by quickly running
my math, that pie
and you can see the little print three, which, of course, is a result of adding one and two.
So that's our model. That's our package. Now let's prove that it worked correctly. It's true that we actually made it
go back to our parent directory here,
and we're going to actually call and invoke it. We're gonna do vim test out pie
user been Python three
and then we're going to imports my mod dot
I'll make sure that I have that program named corrects l s my mod. Yep. Sure enough, my math dot pie. So then test a pie
and then we're really quickly. We're going to invoke
one of those functions that we custom created. We're going to do that
my mom, actually, before I do that one thing that I do want a note when you're using packages and you've got these these nested references, something that you're often going to want to do with your import statement
is import the module as some things with a slightly different name. It just makes it easier to address. So instead of my ma dot mind map, we're going to say, Import my ma dot my math as my math.
That's just a useful shorthand that let us get around having very, very, very long variable names so important. I'm odd, my mouth as my math. And then we're going to say Prince, my math dot add one
And hopefully, if we've done everything correctly when we run this, it's just going to directly run that function and print out the result of adding one in three.
And it did not work because it has no attributes called Ad because, as I recall, I did not name it ad. I named it something else. I named it my ad
and let's try this one last time,
and there we go printed out the number four so you can see the process of creating a module in the process of creating a package is actually much more straightforward than you might suspect.
It's a pretty simple process in which you just create the python code you want to create and then make it easy to reference. And that import as is a very useful tool. It's something that I use pretty often when I'm importing python modules.
It's because those names can get pretty extensive and pretty ridiculous.
So it's gonna be the end of this video and therefore the last piece of material you're gonna learn here. An intermediate python. When you come back for our final video in the series, all we're gonna do is a course summary and review. So thank you for joining me. I have been your instructor, Joe Perry, and you were watching intermediate python on Sai Berry on demand
Course Assessment - Intermediate Python