Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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)

2 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/

3 def f(a): q = 7 z = 13 f(z) global namespace local namespace z a q 13 7 f How arguments are transferred in a function call function object

4 def f(a): q = 7 a = 8 z = 13 f(z) print z global namespace local namespace z aqaq 13 7 How arguments are transferred in a function call 8 f function object

5 Retrieving a variable’s value built_in global local import math def f(a): q = 7 print y x = 9 z = 13 f(z) raw_input, int, float,.. __name__ = ‘__main__’, math, f, x = 9, z = 13,.. a = 13, q = 7 ? NameError: global name ‘y’ is not defined

6 built_in global local import math def f(a): x = 7 print x x = 9 z = 13 f(z) print x raw_input, int, float,.. __name__ = ‘__main__’, math, f, x = 9, z = 13,.. a = 13, x = 7 local x global x Value of x..? 9. Two x ’es

7 built_in global local import math def f(a): global x x = 7 x = 9 z = 13 f(z) print x range, raw_input, int, float,.. __name__ = ‘__main__’, math, f, x =.., z = 13,.. a = 13 global x Value of x..? 7! Keyword global no x here

8 Scope of a variable The region of the program where the variable is accessible Using a variable outside its scope: def area(r): pi = 3.15 return pi*r*r* a = area(1.0) print pi a = area(1.0) def area(r): pi = 3.15 return pi*r*r*  

9 Importing only parts of a module The from/import statement – For importing only a specific part of a module – Takes identifiers from a module and inserts them directly into the current program’s name space (avoids e.g. math. prefix) >>> from math import sqrt >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'sqrt'] >>> >>> sqrt( 9.0 ) 3.0 >>> >>> from math import sin, cos, tan >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'cos', 'sin', 'sqrt', 'tan'] Now directly a part of the current namespace

10 Directly importing all identifiers from a module >>> from math import * >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp','log', 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] Danger! If you already have a log() function with a different purpose (perhaps you are a captain and wrote the function to access the log of your spaceship..), it will be shadowed! Either just import math and use math.log(), or..

11 Binding new names to modules and module functions from import as – imports a function but gives it a different name: from math import log as logarithm You can also give a new name to an entire module: – import math as mathtoolbox

12 Example: a function for computing the factorial of n 1! = 1 2! = 2*1 7! = 7*6*5*4*3*2*1 n! = n*((n-1)!) I.e. the factorial can be defined in terms of itself Algorithm: If n < 2: fac = 1 Otherwise fac = n*fac(n-1)!

13 A recursive function This code is only executed if the file is python’ed directly, not if it is imported. factorial.py Documentation string. Printed by help() if called on this function

14 threonine:~...ProgramExamples% python factorial.py n: 10 3628800 n: 4 24 n: -1 threonine:~...ProgramExamples% 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 Executing file directly: Importing file:

15 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’: myfunction(6, 3) # set a and b, use default for c myfunction(6) # set a, use default for b and c myfunction(6, 3, 7) # set a, override default values

16 def boxVolume( length = 3, width = 2, height = 1 ): return length * width * height 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 ) print "\nThe volume of a box with length 10," print "width 5 and height 1 is:", boxVolume( 10, 5 ) print "\nThe volume of a box with length 10," print "width 5 and height 2 is:", boxVolume( 10, 5, 2 ) 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 All default values usedThe 10 will replace the 3 and the other default values will be used No default values used Here two values are sent replacing the two left-most default values

17 Keyword Arguments – You can use parameter names as keywords: – Allows arguments to be passed in any order: def myfunction( a, b ):.. myfunction ( b = 5, a = 9 )

18 Personal data: George, 53, president (US) Personal data: Tony, 53, prime minister (GB) Personal data: Ronald, 92, president (US) Some of the parameters have default values The parameters are given new values except age which uses the default. Keyword arguments need not be entered in order. Sets first argument, uses the defaults for all the others Uses keywords for two arguments, uses default values for the rest

19 How not to use keyword arguments >>> >>> def test( name, age = 10, town = “Canberra” ):... pass... >>> test( age = 30, “Alice" ) SyntaxError: non-keyword arg after keyword arg >>> >>> test( age = 30 ) Traceback (most recent call last): File " ", line 1, in ? TypeError: test() takes at least 1 non-keyword argument (0 given) Keyword arguments must appear to the right of any other arguments in a function call. Otherwise ambiguity may arise. Is “Alice” the value for name or town ? No value given for name which doesn’t have a default value Rule of thumb: arguments determined by position must come first

20 On to the exercises..


Download ppt "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."

Similar presentations


Ads by Google