Mar 11, 2013

Packages in Python


Today, We discuss about the following :
1) how to create and use a Package in Python with a simple example.
2) Use of __init__.py
3) How to import classes/function and how to use them

Lets take a ride.

Create a folder structure as mentioned below (Where the package name is Package_Sample):
Package_Sample/__init__.py
Package_Sample/A.py
Package_Sample/B.py
Package_Sample/test.py
Package_Sample/sub_folder/C.py
Package_Sample/sub_folder/D.py

Use of __init__.py
In this file, we define the path of all the files & functions to be imported required for the project.
Once you define here, all these classes/functions, you can access directly, we discuss about this in a short while

e.g.,
from A import *
from B import *
from sub_folder.C import *
from sub_folder.D import *

How to use classes defined in __init__.py
Once you have defined in __init__.py
You can directly access functions of C & D (which are in the path Package_Sample/sub_folder/)
Main advantage is that you no need to specify the whole path (sub_folder) again and again.

e.g.,
from Package_Sample import A
from Package_Sample import B
from Package_Sample import C
from Package_Sample import D



Package_Sample/__init__.py

'''
Once you have defined in __init__.py
You can directly access functions of C & D (which are in the path Package_Sample/sub_folder/)
Main advantage is that you no need to specify the whole path (sub_folder) again and again.
'''

from A import *
from B import *
from sub_folder.C import *
from sub_folder.D import *
 


Package_Sample/A.py

class A:
    def __init__(self, type, height, price, age):
        self.type = type
        self.height = height
        self.price = price
        self.age = age
        
    def print_details(self):
        print("\nThis A Type is  :" + self.type + " type ")
        print("This A Height is  :" + self.height + " foot ")
        print("This A Price is   :" , str(self.price) + " dollars ")
        print("This A Age is     :" , str(self.age) + " years")
	 


Package_Sample/B.py

class B:
    def __init__(self, type, height, price, age):
        self.type = type
        self.height = height
        self.price = price
        self.age = age
        
    def print_details(self):
        print("\nThis B Type is    :" + self.type + " type ")
        print("This B Height is    :" + self.height + " foot ")
        print("This B Price is     :" , str(self.price) + " dollars ")
        print("This B Age is       :" , str(self.age) + " years")
        
        	 


Package_Sample/test.py


#Testing __init__.py
from Package_Sample import A 
from Package_Sample import B
from Package_Sample import C
from Package_Sample import D

cfcpiano_A = A("AAAA", "100 Cms", 11000, 10)   #Constructor or Creating Object for Class A
cfcpiano_A.print_details()

cfcpiano_B = B("BBBB", "200 Cms", 22000, 20)   #Constructor or Creating Object for Class B
cfcpiano_B.print_details()

cfcpiano_C = C("CCCC", "300 Cms", 33000, 30)   #Constructor or Creating Object for Class C
cfcpiano_C.print_details()

cfcpiano_D = D("DDDD", "400 Cms", 44000, 40)   #Constructor or Creating Object for Class D
cfcpiano_D.print_details()


	 


Package_Sample/sub_folder/C.py
   
class C:
    def __init__(self, type, height, price, age):
        self.type = type
        self.height = height
        self.price = price
        self.age = age
        
    def print_details(self):
        print("\nThis C Type is  :" + self.type + " type ")
        print("This C Height is  :" + self.height + " foot ")
        print("This C Price is   :" , str(self.price) + " dollars ")
        print("This C Age is     :" , str(self.age) + " years")

    	 


Package_Sample/sub_folder/D.py

class D:
    def __init__(self, type, height, price, age):
        self.type = type
        self.height = height
        self.price = price
        self.age = age
        
    def print_details(self):
        print("\nThis D Type is  :" + self.type + " type ")
        print("This D Height is  :" + self.height + " foot ")
        print("This D Price is   :" , str(self.price) + " dollars ")
        print("This D Age is     :" , str(self.age) + " years")
        	 

How to Run :
Package_Sample/]# python test.py

You may also wish to read Object Oriented Concepts in Python as mentioned :
Python Class and Object Example
Inheritance in Python

No comments:

Post a Comment