Modules in Python
In the past few weeks I have been making an effort to start learning python, and as such I should start getting into how to go about making some kind of real project with python at some point. So far I have been just toying around with small snippets of code, and working out the very basic of python programing. However when it comes to the idea of getting into making something real I am going to want to know how to go about making modules in python. That is how to go about taking code and breaking it down into separate files to which I can then import into another file that will be the main python file or script file that will be started with the python binary one way or another.
So in this post I will be going over the basics and more when it comes to modules and scripts in python as I have come to know it thus far. Such as the difference between a script and a module, how to go about using a module from a script file, and how to go about creating a module of my own.
1 - Basic script example
The first kind of module that comes to mind might not really be a module as it would seem that it is actually referred to often as a script actually. However I think that it is something that I should start off with at least when it comes to the topic of modules. After all when it comes to using modules there is still going to need to be some kind of main file that is called to start a main program of some kind. This main file might not really be a module, but it is of course a way to go about using a module.
1.1 - Basic hello world script
The basic idea of a script is to just save some python code as a file with a py file name extension. That way I do not have to keep typing the same python code over and over again I can just call the file each time. When doing so it might make sense to place a shebang at the top of the file when it comes to this kind of file that will be called directly with the python binary.
So say I have a hello.py file like this:
|
|
I can call it with python like this:
|
|
Or I can make the file executable and call it directly thanks to the shebang
|
|
So that is the basic idea when it comes to a script.
2 - Creating my own module
Now that I have covered the basic of making what a script file is often called, it is time to create some basic modules of my own. In this section I will be quickly going over a few basic examples of making my own modules in python. These modules will be files that just define some functions that can then be used in a script file. I will be starting out with some very basic examples, and the maybe move into something that might start to be the very beginnings of an actual project of some kind, however that might still be a bit of a stretch to call it that.
2.1 - Basic hello world module example
So for this example I have a hello.py file like this:
|
|
This file just creates one function called hello world and that is it. I am not calling the function here, or doing anything more beyond just defining this one simple function. So this hello.py file will be the module, and I will now need a script to make use of this module.
With that I then have a script_hello.py file like this:
|
|
This is then a simple script file that will be called with python and will make use of my hello.py module.
|
|
Okay great so now I have the very basic idea of how to go about making a python module. So now that I have that out of the way i can start working on something that is a little more advanced.
2.2 - Basic display object module example
One thing that will come up often when making some kind of game for example will be to have a way to create some kind of display object. In other words and object that has properties that hold a current position, along with width and height properties that represent some kind of object that will move around in a game world. So maybe I should take a moment to work out a very simple example of that kind of modules as a way to attempt creating some kind of real modules example that might lead to some kind of basic game example at some point in the future.
I think this display object module should have a function that will create some kind of basic display object that just contains the basic set of properties that any display object should have. On top of that I think I should have at least one additional function that will create a different kind of display object, and maybe for now just one function that will create a crude pool of display objects.
|
|
So I can the use my display object module in a script like this.
|
|
Seems to work okay so far, but I would have to add and change a lot to make this something that will actually work as a real python example of some kind. I am still new to python when I put this together, and have not yet got into how to go about making classes in python. Also when it comes to making a real project I might want to get into making something like this be a part of a package of sorts that I need to look into more. Still this might be a good start when it comes to making this kind of module, much would have to change, but maybe only so much actually.
3 - Conclusion
That will be it for now when it comes to modules in python. There is a great deal more to write about on this topic of course, but I am still new to python myself when it comes to this topic so I need to spend a little more time working on some actual code examples when it comes to things like packages. There are also some additional standards and best practices that I did not get to when it comes to making modules, however again I think that should all be part of a future packages section in this post when and if I get around to editing this post a little next time.