10.2 A first module >>> pi Traceback (innermost last): File "", line 1, in ? NameError: name 'pi' is not defined >>> area(2) Traceback (innermost last): File "", line 1, in ? NameError: name 'area' is not defined >>> import mymath >>> pi Traceback (innermost last): File "", 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 "", line 1, in ? NameError: name 'area' is not defined >>> import mymath, imp >>> imp.reload(mymath) 10.5 Private names >>> from modtest import * >>> f(3) 3 >>> _g(3) Traceback (innermost last): File "", line 1, in ? NameError: _g is not defined >>> a 4 >>> _b Traceback (innermost last): File "", line 1, in ? NameError: _b is not defined >>> import modtest >>> modtest._b 2 >>> from modtest import _g >>> _g(5) 5 10.7 Scoping rules >>> locals() f{'__builtins__': , '__name__': '__main__', '__doc__': None, '__package__': None} >>> globals() {'__builtins__': , '__name__': '__main__', '__doc__': None, '__package__': None}>>> >>> z = 2 >>> import math >>> from cmath import cos >>> globals() {'cos': , '__builtins__': , '__package__': None, '__name__': '__main__', 'z': 2, '__doc__': None, 'math': } >>> locals() {'cos': , '__builtins__': , '__package__': None, '__name__': '__main__', 'z': 2, '__doc__': None, 'math': } >>> math.ceil(3.4) 4 >>> del z, math, cos >>> locals() {'__builtins__': , '__package__': None, '__name__': '__main__', '__doc__': None} >>> math.ceil(3.4) Traceback (innermost last): File "", line 1, in NameError: math is not defined >>> import math >>> math.ceil(3.4) 4 >>> def f(x): ... print ("global: ",globals()) ... print ("Entry local: ",locals()) ... y = x ... print ("Exit local: ",locals()) ... >>> z = 2 >>> globals() {'f': , '__builtins__': , '__package__': None, '__name__': '__main__', 'z': 2, '__doc__': None} >>> f(z) global: {'f': , '__builtins__': , '__package__': None, '__name__': '__main__', 'z': 2, '__doc__': None} Entry local: {'x': 2} Exit local: {'y': 2, 'x': 2} >>> >>> import scopetest >>> z = 2 >>> scopetest.f(z) global: ['f', '__builtins__', '__file__', '__package__', 'v', '__name__', '__doc__'] entry local: {'x': 2} exit local: {'y': 2, 'x': 2, 'w': 6} >>> print (max.__doc__) max(iterable[, key=func]) -> value max(a, b, c, ...[, key=func]) -> value With a single iterable argument, return its largest item. With two or more arguments, return the largest argument. >>> >>> list("Peyto Lake") ['P', 'e', 'y', 't', 'o', ' ', 'L', 'a', 'k', 'e'] >>> list = [1,3,5,7] >>> list("Peyto Lake") Traceback (innermost last): File "", line 1, in ? TypeError: 'list' object is not callable >>> import mymath >>> mymath = mymath.area >>> mymath.pi Traceback (most recent call last): File "", line 1, in AttributeError: 'function' object has no attribute 'pi' >>> del list >>> list("Peyto Lake") ['P', 'e', 'y', 't', 'o', ' ', 'L', 'a', 'k', 'e'] >>> import mymath >>> mymath.pi 3.1415899999999999 >>> x1 = 6 >>> xl = x1 - 2 >>> x1 6 >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'x1', 'xl']