Sunday, October 15, 2023

Python Modules

Python Modules

Python Modules

  • Simply a module is a library of code ( Python Code file with extention .py )
  • It’s contains set of functions you want to include in your applications
  • # For Ex – Saving following code in a file with name as mymodule.py
  •      def greeting(name):
    	    print("Hello," + name)
  • Use of an existing module in another python program...
  •      # For Ex –  Save following code in file with name UseOfExistingModule.py
    
            import mymodule               # importing an existing module
            mymodule.greeting("Jonathan") # Calling of specific function
    
    
  • # Note: A module function is called as follow:

          modulename.functionname

Use of variables in modules

  • A module can contain any type of variable
  • Variable type can be : String , Array , List , Set , Tuple , Dictionary etc
    
     #  For Ex - Save following  code in the file  as mymodule.py 
       
     person = {
               "name":"John",
               "age": 36,
               "country:"Norway"
              }

     #  Using above module in another program as follow
       
        import mymodule
        a = mymodule.person["age"] # Accessing age property of person1 Dict.
        print(a)
        
        Output Will be  : 36

Naming And Python Modules


#  You can give any name to the module file but it must have the file extension .py
#  You can create an alias when you import a module, by using the as keyword
#  For Ex – Create an alias for mymodule called mx

    import mymodule as mx
    a = mx.person1["age"] # Use of alias name to call a module function
    print(a)
	
    Output will be : 36

#  Using Built-In Modules : you can use built in module by importing them
#  For Ex –  Import and use the platform built in module

   import platform        # platform is a built in module
   x = platform.system()  # Calling a function of built in module
   print(x)               # This will print name of running OS on system
	
   Output will be  : Windows ( In Case of Windows OS )

Use the dir() Function

  • This is a built in function
  • It is used to list out all the function / variables defined in a module

      # For Ex – List out all the functions and variables  in platform module

      import platform
      x = dir(platform) # Passing module name to list out its contents
      print(x)          # To display contents

      Output will be like this :
         ['DEV_NULL', '_UNIXCONFDIR', 'WIN32_CLIENT_RELEASES', ‘
         WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__',
         Note *: 
         dir() function can be use to list out contents of built in and user defined modules

Use of Import From Module Statement

  • You can import a specific part of the module using the from key word
 
   #  For Ex – First  Define a module as follow and save as mymodule.py
   #  This module define a function and a dictionary 

     def greeting(name):
         print("Hello," + name)
        
     person1 = {
                "name":"John",
                "age": 36,
                "country":"Norway"
               }

   #  Now importing only dictionary as follow in another program
    
      from mymodule import person1  # importing person named dictionary only

      print (person1["age"])   #  Note: No use of module name to access dictionary

      Output Will be : 36

No comments:

Post a Comment