































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































Python Functions & Libraries
(^) Finish the function at any time using return def sign(x): if x < 0: return - if x == 0: return 0 return 1 (^) Functions with return statements scattered through them are hard to understand (^) Have to read the function line by line to figure out what it might do (^) In general: (^) Use early returns at the start of the function to handle special cases (^) And then one return at the end to handle the general case
(^) Python manages variables using a call stack
rock_type = 'unknown'
def classify(rock_name): if rock_name in ['basalt', 'granite']: rock_type = 'igneous' elif rock_name in ['sandstone', 'shale']: rock_type = 'sedimentary' else: rock_type = 'metamorphic' print 'in function, rock_type is', rock_type
print "before function, rock_type is", rock_type classify('sandstone') print "after function, rock_type is", rock_type before function, rock_type is unknown in function, rock_type is sedimentary after function, rock_type is unknown
(^) A table of name/value pairs (^) Parameters are just local variables that are automatically initialized (^) When a variable is referenced, Python looks for it in: (^) The top stack frame, then (^) The global variables Figure l8.1: Call Stack
Figure 8.2: Parameter Passing
(^) …which is a slice of values that includes the entire list… (^) …and slicing creates a new list def add_salt(first, second): first += "salt" second += ["salt"] str = "rock" seq = ["gneiss", "shale"] print "before" print "str is:", str print "seq is:", seq add_salt(str, seq[:]) print "after" print "str is:", str print "seq is:", seq before str is: rock seq is: ['gneiss', 'shale'] after str is: rock seq is: ['gneiss', 'shale']
(^) You can specify default values for parameters when defining a function (^) Just “assign” some value to the parameter in the definition def total(values, start=0, end=None):
if not values: return 0
if end is None: end = len(values)
result = 0 for i in range(start, end): result += values[i] return result
numbers = [10, 20, 30] print "numbers being added:", numbers print "total(numbers, 0, 3):", total(numbers, 0, 3) print "total(numbers, 2):", total(numbers, 2) print "total(numbers):", total(numbers) numbers being added: [10, 20, 30] total(numbers, 0, 3): 60 total(numbers, 2): 30 total(numbers): 60
(^) Otherwise, matching values to parameters would be ambiguous
Figure 8.4: Functions as Objects
(^) Example: apply a function to each value in a list def apply_to_list(function, values): result = [] for v in values: temp = function(v) result.append(temp) return result radii = [0.1, 1.0, 10.0] print apply_to_list(circumference, radii) [0.62831800000000004, 6.2831799999999998, 62.831800000000001]
(^) Every function has an attribute called name (^) The name it was originally defined with (^) Handy when debugging def sedimentary(rock_name): return rock_name in ['sandstone', 'shale'] sed = sedimentary print 'original name:', sedimentary.name print 'name of alias:', sed.name original name: sedimentary name of alias: sedimentary (^) Python uses double underscores to mark reserved names (^) See many more later
(^) Every Python file is automatically also a module (or library) (^) Refer to its contents as geology.thing (^) Just like the methods and attributes of an object (^) Put this in geology.py def rock_type(rock_name): if rock_name in ['basalt', 'granite']: return 'igneous' elif rock_name in ['sandstone', 'shale']: return 'sedimentary' else: return 'metamorphic‘ (^) And this in analysis.py import geology for r in ['granite', 'gneiss']: print r, 'is', geology.rock_type(r) (^) When analysis.py runs, it prints this granite is igneous gneiss is metamorphic