How do i create python module?



  • Let's start by creating a file which will be your module, the file is called cakes.py, in a directory called cakemodule. /cakemodule/cakes.py

    def cakeinfo(desc, baselayer):
        print("Type of cake is: " + desc)
        print("Base layer of the cake is: " + baselayer)
    

    Next, we use that module in another python file, start with the file created in article: https://www.lightnetics.com/post/9890

    import math
    from math import sqrt
    from cakemodule import cake      # import our module.
    
    class MyModuleClass():
        def demo_math_mod(self):
            print(sqrt(100))
    
        def cake4u(self):                
            desc = "fruit"
            baselayer = "sponge"
            cake.cakeinfo(desc, baselayer)  # Use our new module as a method
    
    m1 = MyModuleClass()
    m1.demo_math_mod()
    m1.cake4u()
    

    The out will be as follows; the last two lines were from our new module.
    10.0
    Type of cake is: fruit
    Base layer of the cake is: sponge


Log in to reply
 

© Lightnetics 2024