0% found this document useful (0 votes)
2 views

unit-3 -functions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit-3 -functions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Functions

52
Function Basics

def max(x,y) : >>> import functionbasics


if x < y : >>> max(3,5)
return x 5
else : >>> max('hello', 'there')
return y 'there'
>>> max(3, 'hello')
'hello'
functionbasics.py
Functions are objects
 Can be assigned to a variable
 Can be passed as a parameter

 Can be returned from a function

• Functions are treated like any other


variable in Python, the def statement
simply assigns a function to a variable
Function names are like any
variable
>>> x = 10
>>> x
10
 Functions are >>> def x () :
objects ... print 'hello'
 The same >>> x
<function x at 0x619f0>
reference rules
>>> x()
hold for them as hello
for other objects >>> x = 'blah'
>>> x
'blah'
Functions as Parameters

def foo(f, a) : >>> from funcasparam import *


return f(a) >>> foo(bar, 3)
9
def bar(x) :
return x * x

funcasparam.py Note that the function foo takes two


parameters and applies the first as a
function with the second as its
parameter
Higher-Order Functions

map(func,seq) – for all i, applies func(seq[i]) and returns the


corresponding sequence of the calculated results.

>>> from highorder import *


def double(x): >>> lst = range(10)
return 2*x >>> lst
[0,1,2,3,4,5,6,7,8,9]
highorder.py >>> map(double,lst)
[0,2,4,6,8,10,12,14,16,18]
Higher-Order Functions

filter(boolfunc,seq) – returns a sequence containing all those


items in seq for which boolfunc is True.

>>> from highorder import *


def even(x): >>> lst = range(10)
return ((x%2 == >>> lst
0)
[0,1,2,3,4,5,6,7,8,9]
highorder.py >>> filter(even,lst)
[0,2,4,6,8]
Higher-Order Functions

reduce(func,seq) – applies func to the items of seq, from left


to right, two-at-time, to reduce the seq to a single value.

>>> from highorder import *


def plus(x,y): >>> lst = [‘h’,’e’,’l’,’l’,’o’]

return (x + y) >>> reduce(plus,lst)

‘hello’

highorder.py
Functions Inside Functions

 Since they are like any other object, you


can have functions inside functions

def foo (x,y) : >>> from funcinfunc import *


def bar (z) : >>> foo(2,3)
return z * 2 7
return bar(x) + y

funcinfunc.py
Functions Returning Functions

def foo (x) :


def bar(y) :
return x + y % python funcreturnfunc.py
return bar <function bar at 0x612b0>
# main 5
f = foo(3)
print f
print f(2)

funcreturnfunc.py
Parameters: Defaults

 Parameters can be >>> def foo(x = 3) :


assigned default ... print x
values ...

>>> foo()
 They are
3
overridden if a
>>> foo(10)
parameter is given 10

for them >>> foo('hello')


 The type of the hello

default doesn’t
limit the type of a
parameter
Parameters: Named

 Call by name >>> def foo (a,b,c) :


 Any positional ... print a, b, c
arguments ...
>>> foo(c = 10, a = 2, b = 14)
must come
2 14 10
before named >>> foo(3, c = 2, b = 19)
ones in a call 3 19 2
Anonymous Functions

 A lambda
expression >>> f = lambda x,y : x + y
returns a >>> f(2,3)
function object 5
 The body can >>> lst = ['one', lambda x : x * x, 3]
only be a simple >>> lst[1](4)
expression, not 16
complex
statements
Modules

 The highest level structure of Python


 Each file with the py suffix is a module

 Each module has its own namespace


Modules: Imports

import mymodule Brings all elements


of mymodule in, but
must refer to as
mymodule.<elem>
from mymodule import x Imports x from
mymodule right into
this namespace
from mymodule import * Imports all elements
of mymodule into
this namespace

You might also like