When. This.
- Package Your Python Django Application into a Reusable Component
- Python Programming – Python Packages
- Python Programming – Import Model:
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):
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()