Up to this point, we've learned about a few ways to organize our Python code.
The first thing we did well, we just wrote Python in a file and that organizes it.
Right?
Our entire code has been so far on one single file.
So that was one way that we organized our code.
Next, we learned about functions and functions were used as a way for us to not write code that we
repeat over and over, we could write functions that we can use anywhere we want in our file so that
we can call these functions over and over and we can name the functions appropriately so that our code
makes sense.
And then we learned about classes, write these blueprints that we can create objects from.
We can instantiate new Python data types beyond the ones that they just gave us.
Right.
Instead of just having dictionaries and lists, we can create our own custom once our own lists maybe
that have extra features.
And we're able to use classes to organize our code to box them up and give each class a certain method
and attribute that packages it nicely for us to use.
We also learned about functional programming and how, unlike, let's say, object oriented programming,
where we see a lot of classes with functional programming, we just use a lot of functions, a lot of
pure functions.
And we have data that we just passed into those functions and we organize things into small little pieces.
All of these things were meant for us to keep our code clean, make our code more maintainable, thus
keeping our code organized, because as we write more lines of code, it gets harder and harder to keep
everything in your head.
If other people join your team to work on your project, they need an easy way to figure out how things
are working.
But we still have a problem, and that problem is that everything is contained in this one file, all
the functions, all the classes we've been writing has been on just one dot pi file.
But in real life, this isn't the case.
We have a lot more code and we can't just have one file with millions line of code.
So how do we stay organized?
If we had multiple files of Python.
Like this, because our project is getting bigger and bigger.
Would this work?
Is there a way for us to link all of these files together?
As a matter of fact, there is and this is very common practice all across industry, and we call this
way of organizing code modules.
And modules are simply, well, these files, each one of these files, each dot py file is a module.
And by building these modules, kind of like we built different functions, kind of like we built different
classes, because inside of these files we can have classes, we can have functions, but we can also
have different files.
So a layer higher to divide up our code.
And in this section, we're going to learn all about modules and try to master it and see how we can
write organized code.
Let's have a look, you can see over here, if I click on philes in our Reppel, we have a main DCPI
and every time I click run this fall, I run.
But what if we have more files?
What if I add another file here and we'll call it utility dot pi?
So based on what I just told you, these two things are what they're module's right, main DCPI is a
module utility.
Tappy is a module.
And when writing modules, we use the same principles as variables in Python, which is snake case,
that is, if they're spaces we use underscore everything is lowercase letter.
So how do we divide up these modules and this comes with practice when you're just beginning, it's
hard to say, hey, what goes into what?
What goes into what file?
But it's similar to classes, right?
Similar to functions.
We want to divide up our code into chunks that make sense.
Again, let's say we're working at Netflix.
We might have a file dedicated to the videos.
We might have another file or module dedicated to the login of a user, maybe another file dedicated
to analytics and figuring out recommendations for users.
We want to group these classes and functions together inside of a file that makes sense.
Now, one of the most common ways to use a module is something like a utility.
Let's say we want to create a utility module that is a module that has a lot of what kind of like a
toolbelt, very simple functions that we can use all across our project because we can imagine here
having multiple files.
So let's create a very simple example.
I'm going to say a multiply function exists here.
And this takes a name one and number two.
And in here, we're simply going to return Namwon Times numb to a very simple function and once again,
we'll also have a divide function.
And yes, this is kind of useless because we can just use the plus or to divide operand in Python.
But stick with me here and trying to show you an example.
All right, so number two, I'm going to return number one, divided by number two, nice and simple.
So this.
Is our utility model that has all these functions for us.
Now, notice here that we're actually not doing anything in this file.
I'm simply defining functions, but I'm not running any of the functions.
But let's say we have this utility module and I want to use it in my main pitfall.
How would we go about doing this?
Well, the way we communicate between these two files is quite simple, all we need to do is use the
import command we import and then give it the file name that we want to import.
What do we want to import?
We want to import utility just like that.
And notice that I'm not adding the dot pie because we assume that whatever we're going to import is
going to be a python file.
So we don't need to do that.
Just the name, just like that.
Now let's print utility here.
If I run this.
Whoa, do you see that we have module utility from and then this is a file path that this website generates
for us, so it doesn't really matter.
Now, there's a few things that happened here.
One is that we generated this PAE cache folder, underscore, underscore, PAE cache.
And oh, boy, what is this gibberish?
Hmm, this is this is pretty confusing, right?
Well, this pie cash is created every time we run a file with, let's say, endorsement's.
So when we're using module's.
You see, what Pae Cash does is when we click, run.
The interpreter is going to create this podcast folder and it's going to say, hey, I'm running this
file.
This one may not file anything that may not pay file imports, let's say utility.
In our case, I'm going to cash it.
So see how here it says dot PC.
This is because it's using the C Python interpreter.
Remember, this is the C python, which is the interpreter written, and C, so this is actually a compiled
file so that next time I click run here on my main DCPI, nothing changes because what's going to happen
is instead of loading up utility Dopy, it's going to load up this compiled version of utility because
nothing has changed and utility DCPI.
And this makes things faster when I tried to run Maeng Pie again.
That's what caching is.
Caching is, hey, I'm going to remember this, and this is the compile version, so I don't have to
go through the compilation step again.
But notice that the main file does not get compiled because, well, we run it every time.
Now, if we change the utility function to not have the divide in there, I click run and you don't
notice it here.
But this has now been rerun because we have a different file.
You see that we have multiply here, but not divide.
So let's bring back divide again.
Go back to here, click, run.
And there you go, we have a divide here two now, but this is something that we don't touch.
This just comes underneath the hood with any editor that we use.
And in a few videos, we're going to show you how this also happens when you use something like a pie
chart.
This is just something that editors allow us to do just so our programs can run faster.
So I now have access.
Let's make this a little bit smaller.
I now have access to utility.
And here's the interesting part.
If I do dot here, look at that.
I have access to divide and multiply so that I can multiply numbers.
Let's say two and three.
Click run.
And I am able to use the utility module in my main file.
Very, very cool.
If I do divide and I click run.
I can use this as well.
Very, very cool, and I can import as many times as I want if I had a different file in here and I
want to import another thing, let's say there is a utility to file.
Well, I can import that and import as many things as I want and use them across my files.
How cool is that?
So now we have a way to organize our code and have all these files communicate together, because in
real life we have big projects and these big projects are going to be worked on by different teams,
different programmers.
And we need a way to work with all these files.
And by using the import statements between modules, we're able to use functionality just like we were
able to use the functionality that functions provided us between different files.
And most of the time, if you're working at a company, the first couple of lines on a dot file are
usually import statements because usually you're communicating with other files.
Let's learn some more in the next video.