Python Modules-

  • – Module is a collection of one or set of programs.
  • – “import” keyword is used to include the module into our program.
  • – If two module names are common then it will override the first module with the updated one.

Ex-

Class Name Is First.Py-

def test1():
    print("1st module test1")

def test2():
    print("1st module test2")

Class Name Is Second.Py-

def test1():
    print("2nd module test1")

def test2():
    print("2nd module test2")

Class Name Is Third.Py-

import First, Second
First.test1()
First.test2()
Second.test1()
Second.test2()

Output-
	1st module test1
	1st module test2
	2nd module test1
	2nd module test2

Another Way Of Importing The Modules

  • – We can use “from” keyword to import the module.
  • – When we will use “from” keyword to import the module then there is no need to specify the imported modules name explicitly in the file.

Ex-

-----OneEx.py
class Test1:
    def test1(): # Static method
        print("One-Test1-test1")

class Test2:
    def test2(self): # Instance method
        print("One-Test2-test2")



-------TwoEx.py
class Test1:
    def test1(): # Static method
        print("Two-Test1-test1")

class Test2:
    def test3(self): # Instance method
        print("Two-Test2-test3")
		
-------ThreeEx.py
from TwoEx import Test1, Test2
from OneEx import Test1, Test2

def main():
    Test1.test1()
    obj = Test2()
    # obj.test3() # will give error AttributeError: 'B' object has no attribute 'test3'
    obj.test2()
main()

Output-
	One-Test1-test1
	One-Test2-test2

Notes-

It completely overrides with the latest file if both the modules is having the same class name. So it is recommended to use from if we have unique classes.

Menu