Let's learn about different ways that we can actually import these modules, up until now, we've only
learned about import, but sometimes things can get pretty hectic.
For example, we can actually have another package inside of another package.
If, for example, I create a new folder here and say new python package, we'll call this more.
Shopping click, OK, you see that I've initiated another package inside of a package that has its own
in it file.
And let's move this now to let's do refractor move and we'll move it inside of the more shopping folder,
shall say more.
I click, OK, you'll see that I've moved this module over here, so I have a package inside of a package.
And the way we would access this is to say simply shopping dot more.
Shopping dot, shopping cart.
And let's copy that over and do the same here.
And now when I click run.
You'll see that it still works, but this is getting a little bit too crazy.
You can imagine as we have more and more packages that we constantly have to do this dot, this dot,
and this is getting a little.
Too long and ugly.
So how can we better this?
Well, one way of doing this in a cleaner fashion is to do something like this instead of saying import,
we say from this.
Part, I want to import the function, so here I'm saying, hey, from package, name, package, name,
module, import, whatever function I want.
In our case, we want to import the buy function.
So you see here, I can just do that and in my code.
I can just run by as if it was part of this main file.
If I run this.
You'll see that it still works.
Awesome, so that's definitely cleaner and we can do this with utility as well.
I can say from utility import and actually import the two files or the two functions.
So that is multiply.
And if we want another function, we can just add a comma and say divide.
So now we can print Apple.
Or by Apple as well as divide.
Let's say five by two.
And we can also multiply.
And make sure it's a comma here, because it is a function like this, so that if I run this, you'll
see that.
There we go.
This works as well.
So this is a really nice way to clean up your coat.
You can also do something like this where you say from package, name, package, name.
And then import the entire module, so let's say I want to import the shopping cart module, I can say
import, so package dot package or you could have just had have done one package and import the actual
module shopping cart.
So now I would access it like this shopping cart dot by.
And this is going to work as well.
Now, the reason you might want to do this instead of what we had before, is that sometimes you can
get named collisions.
Let me show you, let's say I had a print and then in here we use the max function, which gives us
it's a built in function, we give it an iterator and it finds the max for us so that if I run this,
you'll see that the max in this list is three.
Well, imagine in our utility function, we have a let's say instead of divide or we add a max that
simply says.
Returns and oops.
Well, if I go back to my main DCPI.
Get Max as my function and I click run.
Well, you see, I get an there.
I get Max takes zero positional arguments, but was given, but one was given because now Max doesn't
mean what we thought we meant, at least in this file we've imported Max and well.
Max has a whole different function now, so we can actually override existing functions, so by using
something like this shopping cart, we make sure that we have only one case that we might have named
collision.
But all the properties or all the methods that shopping art has are connected to a shopping cart.
So instead of having multiple functions that could be overridden, I only have one namespace.
And by the way, this is also one of the reasons that I don't recommend doing something like this,
you can actually do import star.
And what this says is, hey, from the utility module, import everything.
And when you do that.
You'll see that once again we get an air because it imports the max as well, and because we don't even
know what's being imported right now, we can't see it.
We have to go to the file to see it.
When we get an air like this, we're going to be like, what's going on?
I thought, Max is working and it's because of this line.
So it's always good to be explicit and say exactly what you want to import.
But let's remove Max for now, but as you can see, modules can be important in different ways.
My recommendation is to always be explicit and say exactly what you want to import.
And the format usually follows either using the import statement followed by the package module or import
the module, or you can do the from statement to give it package, support package and module if you
want, or you can do a module and then saying import and the specific function or the actual module
itself, many ways of doing it depending on your need.
The key takeaway here is that modules and packages help us have good engineering practices and build
big projects in an organized fashion, yet still have all these files talk to each other.
Right, that was a lot let's learn more in the next video.