Python : Writing & Importing A Module

When you first start learning a language, there are a lot of concepts to learn! By far, the best way to learn is through doing. As such, I often find myself learning something new when I hit a roadblock with my code, such as not knowing how or if I can add a certain feature. This has resulted in a lot of re-factoring, or totally rewriting scripts once I learn a better way to do something. In the beginning, at least for me, it wasn’t clear whether I should write certain portions in their own function, class, or module.

When should you write a module? When you write code that you will want to use in another project! If you’ve solved a problem once, you don’t want to have to solve it again. This can also make your code easier to read.

How about an example?

One of the first projects I made was a basic random stats generator for a D&D character. In the beginning, it only generated basic stats and printed them to the command line. To simulate rolling for each stat, I used the Random module to choose a number between 1 and 20 as seen below. The problem with this however, is you can end up with some very broken characters, that have extremely low or high stats.

stats_generator

This is because the probability of getting a stat 3 or less, and the probability of getting a stat 18-20, is much higher than when you roll dice to determine stats. When you manually create a D&D character, you roll five six-sided dice (d6), and add the highest three rolls together to generate a stat. This way the highest stat you can roll for is 18 and the lowest is 3. Generating stat values you can’t even roll for is a problem! So, I needed to create my own function that would recreate rolling for character stats like so:

roll5dice_module

I created the function in a separate python document called “roll5d6.py” in the same directory as my main character generator. By saving the file in the same directory you are able to import the module and use the functions within. The import statement works the same way as it does for other modules.

import_modulesI created a new function in my script to generate all the stats at once, using the module “roll5d6” and its function “roll_five_d6”.

 

generate_stats_functionIn hindsight, it would have been best to name the module something like “rollpolydice” so that additional functions could be written to roll additional die types and quantities. In that case, one could choose to import only the functions used in the script. The syntax would be:

  from rollpolydice import roll_five_d6

I hope this example was helpful to you! Please comment if you have any questions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s