# There are three wats to define a module in Python: in Python, in C, and using built in modules # To access contents, use the import keyword import mod print(mod.s) mod.hello('Jason') j = mod.Person('James') j.sayName() # When the interoreter executes an imoprt statement it searches for the mod.py in a list of directories that are curated from the following sources: The directory the script was called in, the list of directories from the PYTHONPATH env variable set in the os, an installation-dependent list configured at the time Python was installed # If you only want to import certain things from a file, use the following code # from <module_name> imort <name(s)? # import pkg.mod1, pkg.mod2 # pkg.mod1.speak() # Package initialization from pkg import * mod1.speak() mod2.talk()
# -*- coding: utf-8 -*- """ Created on Tue Jul 10 14:13:04 2018 @author: user """ import mod a = mod.hello() print(a) def mydef(name): print('yo', name) def mydef2(name): print('hi' + name) def mydef3(name='noname'): print('hi', name) def redef(x): count = x * 10 return count mydef('ho') mydef2('hola') mydef(6)
# ex7.2.py # Create your own module in which to implement at least 3 functions. # Use these functions in a script. import mod print(mod.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) mod.hello() mod.ItsWednesday()
def run(): mod.hello()