Creating modules

Based on what we learned in my blog posts before creating a module can be done by writing a python file and import it to your main program (remember that both files have to be in the same directory!).

Imagine you want to use a function that prints a list of your favorite soccer players. And create it like this:

But now you are changing your mind and you think that it is more valuable to print a list of the best all times players. But you are in a hurry, because your soccer game starts in two hours so you don’t want to write the function again anymore. Solution –> import the module to your main program:

Notice the following: The compiler will run through the whole module before it imports the function to the main module. So if you would uncomment the “#my_favoriteplayers(bestones)” in the first module, it would print the first list as well and then print the list from the main module under it.

So in general, I would recommend to not return the function in the module you wan’t to import as it’s probably not the one you want to see in the main module.

Probably the two best players of two centuries.


Leave a Reply

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

You are commenting using your 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