Advanced scripting programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Advanced scripting programming

Description:

X = 99 # X and func assigned in module: global ... print X. result? Advanced scripting programming lectures 7 and 8. MSc Bioinformatics ... – PowerPoint PPT presentation

Number of Views:139
Avg rating:3.0/5.0
Slides: 27
Provided by: sgam1
Category:

less

Transcript and Presenter's Notes

Title: Advanced scripting programming


1
Advanced scripting programming
  • jesus.ibanez_at_upf.edu

2
Functions
  • In simple terms, functions are a device that
    groups a bunch of statements, so they can be run
    more than once in a program. Functions also let
    us specify parameters, which may differ each time
    a function's code is run.
  • Why use functions?
  • Code reuse
  • Procedural decomposition

3
Functions
def (arg1, arg2,... argN)
return
4
Definition and call
  • Dynamically typed
  • Definition
  • def times(x, y) create and assign
    function
  • ... return x y body executed when
    called
  • ...
  • Call
  • times(2, 4) arguments in
    parentheses
  • 8
  • times('Ni', 4) functions are
    'typeless'
  • 'NiNiNiNi'

5
Definition and call
  • Example intersecting sequences (definition)
  • def intersect(seq1, seq2)
  • res start empty
  • for x in seq1 scan seq1
  • if x in seq2 common item?
  • res.append(x) add to end
  • return res

6
Definition and call
  • Example intersecting sequences (call)
  • s1 "SPAM"
  • s2 "SCAM"
  • intersect(s1, s2) strings
  • 'S', 'A', 'M'
  • intersect(1, 2, 3, (1, 4)) mixed types
  • 1

7
Scope rules
  • The enclosing module is a global scope
  • Each call to a function is a new local scope
  • Assigned names are local, unless declared global
  • All other names are global or built-in

8
Scope rules
9
Scope rules
  • global scope
  • X 99 X and func assigned in
    module global
  • def func(Y) Y and Z assigned in
    function locals
  • local scope
  • Z X Y X is not assigned, so
    it's a global
  • return Z
  • print func(1) func in module result100

10
Scope rules
  • global scope
  • X 99 X and func assigned in
    module global
  • def func(Y) X, Y and Z assigned in
    function locals
  • local scope
  • X 2
  • Z X Y
  • return Z
  • print func(1)
  • print X
  • result?

11
Scope rules
  • global scope
  • X 99 X and func assigned in
    module global
  • def func(Y) X, Y and Z assigned in
    function locals
  • local scope
  • X 2
  • Z X Y
  • return Z
  • print func(1)
  • print X
  • result?

3 99
12
Scope rules
  • The global statement tells Python that a function
    plans to change global namesnames that live in
    the enclosing module's scope (namespace).

13
Scope rules
  • global scope
  • X 99 X and func assigned in
    module global
  • def func(Y)
  • local scope
  • global X
  • X 2
  • Z X Y
  • return Z
  • print func(1)
  • print X
  • result?

14
Scope rules
  • global scope
  • X 99 X and func assigned in
    module global
  • def func(Y)
  • local scope
  • global X
  • X 2
  • Z X Y
  • return Z
  • print func(1)
  • print X
  • result?

3 2
15
Argument passing
  • Immutable arguments act like C's "by value" mode
  • Mutable arguments act like C's "by pointer" mode

16
Argument passing
  • def changer(x, y)
  • ... x 2 changes local name's
    value only
  • ... y0 'spam' changes shared object
    in place
  • ...
  • X 1
  • L 1, 2
  • changer(X, L) pass immutable and
    mutable
  • X, L X unchanged, L is
    different
  • (1, 'spam', 2)

17
Argument passing
  • because return sends back any sort of object, it
    can return multiple
  • values, by packaging them in a tuple.
  • def multiple(x, y)
  • ... x 2 changes local
    names only
  • ... y 3, 4
  • ... return x, y return new
    values in a tuple
  • ...
  • X 1
  • L 1, 2
  • X, L multiple(X, L) assign results
    to caller's names
  • X, L
  • (2, 3, 4)

18
The map built-in
  • One of the more common things programs do with
    lists is to apply an operation to each node and
    collect the results
  • counters 1, 2, 3, 4
  • updated
  • for x in counters
  • ... updated.append(x 10) add 10
    to each item
  • ...
  • updated
  • 11, 12, 13, 14

19
The map built-in
  • the map function applies a passed-in function to
    each item in a sequence object and returns a list
    containing all the function call results
  • def inc(x) return x 10
    function to be run
  • ...
  • map(inc, counters) collect
    results
  • 11, 12, 13, 14

20
Procedures
  • In Python functions, return statements are
    optional. When a function doesn't return a value
    explicitly, the function exits when control falls
    off the end. Technically, all functions return a
    value if you don't provide a return, your
    function returns the None object automatically
  • def proc(x)
  • ... print x no return is a None
    return
  • ...
  • x proc('testing 123...')
  • testing 123...
  • print x
  • None

21
Procedures
  • Be careful. For instance, assigning the result of
    a list append method won't raise an error, but
    you'll really get back None, not the modified
    list
  • list 1, 2, 3
  • list list.append(4) append is a
    'procedure'
  • print list append changes
    list in-place
  • None

22
Function design concepts
  • Use arguments for inputs and return for outputs
  • Generally speaking, you should strive to make
    a function independent of things outside of it.
    Arguments and return statements are often the
    best way to isolate dependencies.
  • Use global variables only when absolutely
    necessary
  • Global variables (i.e., names in the enclosing
    module) are usually a poor way to communicate
    with a function. They can create dependencies
    that make programs difficult to change.

23
Exercises
  • 1) Write a function called copyDict(dict) that
    copies its dictionary argument. It should return
    a new dictionary with all the items in its
    argument. Use the dictionary keys method to
    iterate.

24
Exercises
  • 2) Write a function called addDict(dict1, dict2)
    that computes the union of two dictionaries. It
    should return a new dictionary, with all the
    items in both its arguments (assumed to be
    dictionaries). If the same key appears in both
    arguments, feel free to pick a value from either.

25
Solutions
  • 1)
  • def copyDict(old)
  • new
  • for key in old.keys()
  • newkey oldkey
  • return new

26
Solutions
  • 2)
  • def addDict(d1, d2)
  • new
  • for key in d1.keys()
  • newkey d1key
  • for key in d2.keys()
  • newkey d2key
  • return new
Write a Comment
User Comments (0)
About PowerShow.com