forked from nagesh21/Python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHow to create package?
More file actions
70 lines (47 loc) · 2.98 KB
/
How to create package?
File metadata and controls
70 lines (47 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Steps to Create a Python Package
Working with Python packages is really simple. All you need to do is:
Create a directory and give it your package's name.
Put your classes in it.
Create a __init__.py file in the directory
That's all! In order to create a Python package, it is very easy. The __init__.py file is necessary because with this file, Python will know that this directory is a Python package directory other than an ordinary directory (or folder – whatever you want to call it). Anyway, it is in this file where we'll write some import statements to import classes from our brand new package.
Example On How to Create a Python Package
In this tutorial, we will create an Animals package – which simply contains two module files named Mammals and Birds, containing the Mammals and Birds classes, respectively.
Step 1: Create the Package Directory
So, first we create a directory named Animals.
Step 2: Add Classes
Now, we create the two classes for our package. First, create a file named Mammals.py inside the Animals directory and put the following code in it:
class Mammals:
def __init__(self):
''' Constructor for this class. '''
# Create some member animals
self.members = ['Tiger', 'Elephant', 'Wild Cat']
def printMembers(self):
print('Printing members of the Mammals class')
for member in self.members:
print('\t%s ' % member)
The code is pretty much self-explanatory! The class has a property named members – which is a list of some mammals we might be interested in. It also has a method named printMembers which simply prints the list of mammals of this class! Remember, when you create a Python package, all classes must be capable of being imported, and won't be executed directly.
Next we create another class named Birds. Create a file named Birds.py inside the Animals directory and put the following code in it:
class Birds:
def __init__(self):
''' Constructor for this class. '''
# Create some member animals
self.members = ['Sparrow', 'Robin', 'Duck']
def printMembers(self):
print('Printing members of the Birds class')
for member in self.members:
print('\t%s ' % member)
This code is similar to the code we presented for the Mammals class.
Step 3: Add the __init__.py File
Finally, we create a file named __init__.py inside the Animals directory and put the following code in it:
from Mammals import Mammals
from Birds import Birds
That's it! That's all there is to it when you create a Python package. For testing, we create a simple file named test.py in the same directory where the Animals directory is located. We place the following code in the test.py file:
# Import classes from your brand new package
from Animals import Mammals
from Animals import Birds
# Create an object of Mammals class & call a method of it
myMammal = Mammals()
myMammal.printMembers()
# Create an object of Birds class & call a method of it
myBird = Birds()
myBird.printMembers()