• Save
Python & Perl: Lecture 12
Upcoming SlideShare
Loading in...5
×
 

Python & Perl: Lecture 12

on

  • 796 views

 

Statistics

Views

Total Views
796
Views on SlideShare
449
Embed Views
347

Actions

Likes
0
Downloads
0
Comments
0

20 Embeds 347

http://vkedco.blogspot.com 279
http://www.vkedco.blogspot.com 28
http://vkedco.blogspot.in 8
http://vkedco.blogspot.co.il 5
http://vkedco.blogspot.com.au 4
http://vkedco.blogspot.nl 3
http://www.vkedco.blogspot.in 3
http://vkedco.blogspot.hu 2
http://vkedco.blogspot.kr 2
http://vkedco.blogspot.pt 2
http://vkedco.blogspot.fr 2
http://vkedco.blogspot.sg 1
http://vkedco.blogspot.ru 1
http://vkedco.blogspot.co.nz 1
http://vkedco.blogspot.be 1
http://vkedco.blogspot.de 1
http://vkedco.blogspot.it 1
http://vkedco.blogspot.ca 1
http://vkedco.blogspot.ro 1
http://vkedco.blogspot.jp 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Python & Perl: Lecture 12 Python & Perl: Lecture 12 Presentation Transcript

    • Python & Perl Lecture 12 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Function Parameters ● Scoping ● Functional Programmingwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Function Parameterswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Types of Parameters ● There are two types of parameters in Python: positional and keyword ● Positional parameters are regular parameters in the function signature: the values they receive are determined by their position in the signature ● Keyword parameters are parameters that can taken on default values and whose position can vary in the functional signaturewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Positional Parameters: Example def hw_avail_str(crsn, hwn, hw_loc, due_date, submit_loc): print "%s Assignment %s is available at %s. It is due by %s in %s." % (crsn, hwn, hw_loc, due_date, submit_loc) >>> hw_avail_str(CS3430, 7, www.myurl.org, 11:59pm, Feb 10, 2012, your Dropbox folder) CS3430 Assignment 7 is available at www.myurl.org. It is due 11:59pm, Feb 10, 2012 in your Dropbox folder. ● Note: You have to remember the position of each parameter in the signature, i.e., that crsn (course number) comes first, hwn (homework number) comes second, hw_loc (homework web location) comes third, etc.www.youtube.com/vkedco www.vkedco.blogspot.com
    • Keyword Parameters: Example def hw_avail_str2(crsn=CS3430, hwn=0, hw_loc=www.myblog.org due_date=, submit_loc=your Dropbox folder): print "%s Assignment %s is available at %s. It is due by %s in %s." % (crsn, hwn, hw_loc, due_date, submit_loc) >>> hw_avail_str2(hwn=10, due_date=11:59pm, Feb 10, 2012) CS3430 Assignment 10 is available at www.myblog.org. It is due by 11:59pm, Feb 10, 2012 in your Dropbox folder. ● Note: You do not have to remember the position of each parameter in the signature but you do have to remember the name of each keyword parameterwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Combining Positional & Keyword Parameters ● Positional & keyword parameters can be combined in one signature: positional parameters must precede keyword parameters def combine(x, y=10, z=buzz): print x=%d y=%d z=%s % (x, y, z) >>> combine(5) x=5 y=10 z=buzz >>> combine(5, y=50, z=foo) x=5 y=50 z=foowww.youtube.com/vkedco www.vkedco.blogspot.com
    • Positional Parameter Collection ● What happens if you do not know how many parameter values you receive on the input? ● There are two choices: use a container, e.g., a list or a tuple, or use *operator ● Suppose that we want to write a function that applies three types of operations: sum, product, and max to sequences ● We can use *operator to solve this problemwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Applying Operations to Parameter Collections def apply_oper(oper_name, *params): if oper_name == sum: return sum(params) elif oper_name == product: return reduce(lambda x, y: x * y, params, 1) elif oper_name == max: if len(params) > 0: return max(params) else: return None else: return Unknown operation + oper_namewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Applying Operations to Parameter Collections >>> apply_oper(sum, 1, 2) 3 >>> apply_oper(sum, 1, 2, 3) 6 >>> apply_oper(sum) 0 >>> apply_oper(product, 1, 2, 3) 6 >>> apply_oper(max, 1, 2, 3, 4, 5) 5 >>> apply_oper(max)www.youtube.com/vkedco www.vkedco.blogspot.com
    • Keyword Parameter Collection ● We can also collect arbitrary numbers of keyword parameters with **operator ● The keyword parameters are collected into a dictionary def collect_keyword_params(**prms): print prms >>> collect_keyword_params(x=5,y=10, my_kp=foo, my_kp2=(1, 2, 3)) {y: 10, x: 5, my_kp2: (1, 2, 3), my_kp: foo}www.youtube.com/vkedco www.vkedco.blogspot.com
    • Parameter Distribution ● * & ** operators can be used in the reverse direction as well – to extract the values from sequences or dictionaries and distribute them among the function arguments ● *operator is used with sequences ● **operator is used with dictionarieswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example def add3(x, y, z): return x + y + z def add4(x=0, y=0, z=0, w=0): return x+y+z+w >>> prms = (10, 20, 30) >>> add3(*prms) ## x = 10, y = 20, z = 30 60 >>> prms2 = {x : 10, y : 20, z : 30, w : 40} >>> add4(**prms2) ## x = 10, y=20, z=30, w=40 100www.youtube.com/vkedco www.vkedco.blogspot.com
    • Scopingwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Scope ● Scopes (aka namespaces) are dictionaries that map variables to their values ● Builtin function vars() returns the current scope ● Python documentation recommends against modifying the returned value of vars() because the results are undefinedwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Scope ● def always introduces a new scope ● Here is a quick def scoping example: >>> x = 10 >>> def f(y): x = y ## changes x to y only inside f >>> f(20) >>> x ## we are outside the scope of f so x is 10 10www.youtube.com/vkedco www.vkedco.blogspot.com
    • Scope ● It is possible to change the value of a global variable inside a function: all you have to do is to tell Python that the variable is global def change_x_val(z): global x x=z >>> x = 1 >>> change_x_val(10) >>> x 10www.youtube.com/vkedco www.vkedco.blogspot.com
    • Scope Sensitivity of vars() ● Calls to vars() are scope-sensitive and return the currently active scopes def local_vars2(x, y): def local_vars3(n, m): n=x+1 m=y+2 print vars() inside local_vars3 print vars() z = x + 10 w = y + 20 print vars() inside local_vars2 print vars() local_vars3(3, 4)www.youtube.com/vkedco www.vkedco.blogspot.com
    • Scope Sensitivity of vars() >>> local_vars2(1, 2) vars() inside local_vars2 {y: 2, x: 1, local_vars3: <function local_vars3 at 0x020B10B0>, z: 11, w: 22} vars() inside local_vars3 {y: 2, x: 1, m: 4, n: 2}www.youtube.com/vkedco www.vkedco.blogspot.com
    • Global Scope ● Builtin function globals() returns the global scope regardless of what the current scope is ● For example, the following function prints the global scope def global_vars(x, y): z=x+1 w=y+2 print globals()www.youtube.com/vkedco www.vkedco.blogspot.com
    • Global Scope ● This function prints the global scope several times despite the scope nestings def global_vars2(x, y): def global_vars3(n, m): n=x+1 m=y+2 print globals() inside global_vars3, globals() z = x + 10 w = y + 20 print globals() inside global_vars2, globals() global_vars3(3, 4)www.youtube.com/vkedco www.vkedco.blogspot.com
    • Nested Scopes ● Nested scopes are very useful in functions that return other functions def func_factory(oper, lst): def list_extender(inner_lst): inner_lst.extend(lst) def list_remover(inner_lst): for i in lst: inner_lst.remove(i) if oper == extend: return list_extender elif oper == remove: return list_remover else: return ERROR: Unknown operationwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Nested Scopes ● Nested scopes are very useful in functions that return other functions >>> fex = func_factory(extend, [I, am, extended]) >>> frm = func_factory(remove, [I, am, extended]) >>> lst = [1, 2, 3] >>> fex(lst) ## lst is extended by [I, am, extended] >>> lst [1, 2, 3, I, am, extended] >>> frm(lst) ## all elements of [I, am, extended] are removed from lst >>> lst [1, 2, 3]www.youtube.com/vkedco www.vkedco.blogspot.com
    • Functional Programmingwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Functional Programming ● There is support for functional programming, a programming style advocated most prominently in Common Lisp and Scheme ● In functional programming, functions are treated like any other objects: functions take functions as parameters and apply them to values, functions create and return new functionswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Three Pillars of Functional Programming ● map(func, sequence) ● reduce(fun, sequence, init_rslt) ● filter(fun, sequence)www.youtube.com/vkedco www.vkedco.blogspot.com
    • map(function, sequence [,sequence,...]) → list >>> map(lambda x: x+1, (1, 2, 3)) [2, 3, 4] >>> map(lambda x: x+2, [1, 2, 3]) [3, 4, 5] >>> map(lambda c: c+a, [a, b, c]) [aa, ba, ca] >>> map(str, (1, 2, 3)) [1, 2, 3] >>> def add2(x, y): return x + y >>> map(add2, [1, 2, 3], (4, 5, 6)) [5, 7, 9]www.youtube.com/vkedco www.vkedco.blogspot.com
    • reduce(func, sequence [,initial]) → value ● When not supplied, initial defaults to 0 ● reduce(func, sequence [, initial]) is equivalent to func(func(func(func(seq[0], initial), seq[1]), seq[2]), …, seq[n-1]) , where n is the number of elements in seq ● reduce(lambda x, y: x+y, [1, 2, 3]) is equivalent to (lambda x, y: x + y)((lambda x, y: x + y)((lambda x, y: x + y)(1, 0), 2), 3) ● reduce(add2, [1, 2, 3]) is equivalent to add2(add2(add2(1, 0), 2), 3)www.youtube.com/vkedco www.vkedco.blogspot.com
    • filter(func or None, seq) → list, tuple, string ● filter takes a boolean function func or None and sequence and returns another sequence that contains the elements of the sequences for which func returns True >>> filter(lambda x: x % 2 == 0, xrange(11)) [0, 2, 4, 6, 8, 10] >>> def g(c): return c.isalnum() >>> filter(g, [!, 1, ?, a, @]) [1, a] >>> filter(g, (!, 1, ?, a, @)) (1, a)www.youtube.com/vkedco www.vkedco.blogspot.com
    • Reading & References ● www.python.org ● Ch 06 M. L. Hetland. Beginning Python From Novice to Pro- nd fessional, 2 Ed., APRESSwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Feedback Bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com