Setting the PYTHONPATH • PYTHONPATH is where Python looks for modules it is told to import • List of paths • Add new path to the end with: setenv PYTHONPATH ${PYTHONPATH}:/users/chili/Pythonmodules/ export PYTHONPATH=${PYTHONPATH}:/users/chili/Pythonmodules/ (C shell and Bash, respectively) Permanently adding path to PYTHONPATH • Create/load file called ~/.daimi-setup/tcsh/tcshrc.d/path.rc • Add the magic line to it • To later add more paths: setenv PYTHONPATH ${PYTHONPATH}:/users/chili/Pythonmodules/:/users/chili /Pythonmodules/RealGoodies/ How arguments are transferred in a function call function object global namespace def f(a): q = 7 z = 13 f z 13 local namespace f(z) a q 7 How arguments are transferred in a function call function object global namespace def f(a): q = 7 a = 8 z = 13 f z 13 local namespace f(z) print z a q 8 7 Retrieving a variable’s value import math def f(a): q = 7 ? print y built_in global raw_input, int, float, .. __name__ = ‘__main__’, math, f, x = 9, local z = 13, .. a = 13, q = 7 x = 9 z = 13 f(z) Two x’es built_in import math global def f(a): x = 7 print x raw_input, int, float, .. x = 9 z = 13 __name__ = ‘__main__’, math, f, x = 9, local z = 13, .. a = 13, x = 7 global x f(z) local x print x Value of x ..? 9. Keyword global built_in import math global def f(a): global x x = 7 range, raw_input, int, float, .. __name__ = ‘__main__’, math, f, x = .., local z = 13, .. a = 13 x = 9 z = 13 global x f(z) print x Value of x..? 7! no x here Scope of a variable The region of the program where the variable is accessible Using a variable outside its scope: def area(r): a = area(1.0) pi = 3.15 return pi*r*r* def area(r): pi = 3.15 a = area(1.0) print pi return pi*r*r* Example: a function for computing the factorial of n 1! 2! 7! n! = = = = 1 2*1 7*6*5*4*3*2*1 n*((n-1)!) I.e. the factorial can be defined in terms of itself Algorithm: If n < 2: return n Otherwise return n*((n-1)! demo A recursive function factorial.py Documentation string. Printed by help() if called on this function This code is only executed if the file is python’ed directly, not if it is imported. demo Executing file directly: threonine:~...ProgramExamples% python factorial.py n: 10 3628800 n: 4 24 n: -1 threonine:~...ProgramExamples% Importing file: threonine:~...ExamplePrograms% python Python 2.3.4 (#1, Feb 2 2005, 12:11:53) [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> from factorial import recursive_factorial as f >>> f(5) >>> 120 Default Arguments A function may sometimes be called repeatedly with the same values – Default arguments can be set in function definition • Must appear to the right of undefaulted parameters: def myfunction( a, b = 2, c = 3 ): • Given values are ‘filled in from the left’ (resolves ambiguity) myfunction(6, 3) # set a and b, use default for c myfunction(6) # set a, use default for b and c – A default value can also be overridden: myfunction(6, 3, 7) # overrides default c value def boxVolume( length = 3, width = 2, height = 1 ): return length * width * height All default values used print "The default box volume is:", boxVolume() print "\nThe volume of a box with length 10," print "width 2 and height 1 is:", boxVolume( 10 ) The 10 will replace the 3 and the other default values will be used Here two values are print "\nThe volume of a box with length 10," print "width 5 and height 1 is:", boxVolume( 10, 5 ) sent replacing the two left-most default values print "\nThe volume of a box with length 10," print "width 5 and height 2 is:", boxVolume( 10, 5, 2 ) No default values used The default box volume is: 6 The volume of a box with length 10, width 2 and height 1 is: 20 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100 Keyword Arguments – You can use argument names as keywords: – Allows arguments to be passed in any order: def myfunction( a = 2, b = 3 ): .. myfunction ( b = 5, a = 9 ) Some of the parameters have default values Sets first argument, uses the defaults for all the others name can be used as keyword as well, even though it doesn’t have a default value Personal data: George, 53, president (US) Personal data: Tony, 53, prime minister (GB) Personal data: Ronald, 92, president (US) The parameters are given new values except age which uses the default. Values need not be entered in order. How not to use keyword arguments Keyword arguments must appear to the right of any other arguments in a function call. Otherwise ambiguity may arise. >>> >>> def test( name, age = 10, town = “Canberra” ): ... pass ... Is “Alice” the value for >>> test( age = 30, “Alice" ) name or town? SyntaxError: non-keyword arg after keyword arg >>> >>> No value given for name which >>> test( age = 30 ) Traceback (most recent call last): doesn’t have a default value File "<stdin>", line 1, in ? TypeError: test() takes at least 1 non-keyword argument (0 given) Rule of thumb: arguments determined by position must come first On to the exercises..