Published on

Basic Example with Python to create a Module that consists of Classes and Functions


Just a very basic example how to create a Python Module that consists of a Single Class and 2 basic functions.

Our main app will can our module to print out a word, that we pass to our first function.

The Directory Setup:

Below is a tree view of my current working directory:

$ tree
├── providers
│   ├──
│   ├──

In order to make a python file a module, we need to have a blank file in our directory. So any files under our providers directory will be seen as modules from our file.

Our Test Module:

in our providers/ file:

class TestClass:
    def word_to_return(self, word_value):
        return word_value
    def simple_test(self):
        data = self.word_to_return('its me!')
        return data

Then our providers/ file will be blank.

Our, we will import our test module, instantiate our class, and call our function within the class that we instantiated:

from providers import test

test_instance = test.TestClass()
response = test_instance.simple_test()


instead of response = test_instance.simple_test(), you could also do print(test_instance.simple_test()

Testing it out:

$ python
its me!

It's very basic but will post some more topics around this in the future.

Also note, this blog is for quick posts that I come accross during my daily doings, for more details tutorials have a look at my main blog:

Thank You

Thanks for reading, feel free to check out my website, feel free to subscribe to my newsletter or follow me at @ruanbekker on Twitter.