""" My final application :use user defined modules """ import mymath #import creates mymath.pyc and module name of mymath chnages from main to mymath print "********************************************************" print "area of circle with radius 2 = ", mymath.area(2) #fib(21) #NameError: name 'fib' is not defined mymath.fib(21)
"""at import statement level if __name__ == "__main__": of module file will not be executed """ import mymath #mymath.pyc print("*********************************") print("pi = ", mymath.pi) print("area = ", mymath.area(5)) mymath.fib(35) #print pi print("Learning Modules") print("End")
#!/usr/bin/python3 import mymath print("{0}".format(mymath.pi)) print("{0}".format(mymath.area(2))) mymath.__doc__ mymath.area.__doc__
#-*- coding: utf-8 -*- import mymath print dir(mymath) print mymath.mypi print mymath.area(5) print mymath.add(3,5)
from mymath import area,pi print "********************************************************" print "area of circle with radius 2 = ",area(2) #fib(21) #NameError: name 'fib' is not defined --> because only area is imported print "++++++++++++++++++++++++++++++++++++++++++++++++++++++=" from math import sqrt sqrt(5) #pow(2,3) #NameError import math math.sqrt(5)
Traceback (innermost last): File "<stdin>", line 1, in ? NameError: name 'pi' is not defined >>> area(2) Traceback (innermost last): File "<stdin>", line 1, in ? NameError: name 'area' is not defined >>> import mymath >>> pi Traceback (innermost last): File "<stdin>", line 1, in ? NameError: name 'pi' is not defined >>> mymath.pi 3.1415899999999999 >>> mymath.area(2) 12.56636 >>> mymath.__doc__ 'mymath - our example math module' >>> mymath.area.__doc__ 'area(r): return the area of a circle with radius r.' >>> from mymath import pi >>> pi 3.1415899999999999 >>> area(2) Traceback (innermost last): File "<stdin>", line 1, in ? NameError: name 'area' is not defined >>> import mymath, imp
import mymath import mymath # will not print anything # pi # NameError -> could be solved via __init__ print(mymath.pi) # 3.14159 print(mymath.area(2)) # 12.56636 print(mymath.__doc__) # 'mymath - our example math module' print(mymath.area.__doc__ ) # 'area(r): return the area of a circle with radius r.' from mymath import pi print(pi) # 3.14159 from math import pi print(pi) # 3.141592653589793 import math print(mymath.pi, math.pi) # 3.14159 3.141592653589793
def F(): x = 1 def G(): print x G() F() import mymath print mymath.mypi print mymath.add(1, 10) print mymath.area(10) #from tkinter import * #root.mainloop() #root.mainloop() import string print dir(string) string.a = 1 print string.a mymath.a = 1 print mymath.a