Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå

реклама
Python: ôóíêöèè, ôóíêöèîíàëüíîå
ïðîãðàììèðîâàíèå
Àëåêñàíäð Ìèõàéëîâ
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè
Ïðîñòåéøàÿ ôóíêöèÿ íà Python:
> > def empty_func () :
..
pass
Ôóíêöèè îáúÿâëÿþòñÿ ñ êëþ÷åâûì ñëîâîì def
Ôóíêöèÿ - ýòî îáúåêò
Ïóñòûå ôóíêöèè íåäîïóñòèìû
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè
Áîëåå ñëîæíàÿ ôóíêöèÿ
> > def gcd (a , b ) :
..
""" Greatest Common
..
Divisor """ # docstring
..
while a ! = 0 :
..
a , b = b %a , a
..
return b
docstring ìîæíî ïîëó÷èòü òàê:
print gcd . __doc__
Ôóíêöèÿ áåç return âîçâðàùàåò ñïåöèàëüíîå çíà÷åíèå None
Ïàðàìåòðû â ôóíêöèè ïåðåäàþòñÿ ïî-õèòðîìó
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ïåðåäà÷à ïàðàìåòðîâ
Call by object, call by sharing, call by object reference
> > def magic ( v ) :
..
v . append ( " Blue " )
..
> > list = [ " Red " , " Green "]
> > magic ( list )
> > print list
[ ' Red ' , ' Green ' , ' Blue ']
> > def magic2 ( v ) :
..
v = [ " Hue " , " Saturation " , " Value " ]
..
> > magic2 ( list )
> > print list
[ ' Red ' , ' Green ' , ' Blue ']
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè
Ôóíêöèè ìîæíî ïðèñâàèâàòü:
> > d = gcd
> > print d ( 14 , 7 )
7
Áëàãîäàðÿ êîðòåæàì, èç ôóíêöèè ìîæíî âîçâðàùàòü
ìíîæåñòâî ýëåìåíòîâ:
> > def multiout () :
..
return 1 , 2 , 3
> > print multiout ()
(1 , 2 , 3 )
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Îáëàñòü âèäèìîñòè - Scope
 ëþáîé ìîìåíò âûïîíåíèÿ ïðîãðàììû ñóùåñòâóåò, êàê
ìèíèìóì, 3 îáëàñòè âèäèìîñòè:
Ëîêàëüíàÿ
Ñðåäíÿÿ (ãëîáàëüíûå èìåíà ìîäóëÿ)
Âíåøíÿÿ (âñòðîåííûå èìåíà)
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè
Ïåðåäà÷à íåèçâåñòíîãî êîëè÷åñòâà ïàðàìåòðîâ:
> > def avg ( * args ) :
..
sum = 0 . 0
..
for arg in args :
..
sum + = arg
..
return sum / len ( args )
..
> > avg (1 , 2 )
1.5
> > avg (3 , 5 , 2 )
3 . 3333333333333335
Òàêæå ìîæíî ïåðåäàâàòü èìåíîâàííûå ïàðàìåòðû. Â òàêîì
ñëó÷àå, èçìåíÿåòñÿ ñèãíàòóðà:
> > def avg ( * * args ) :
Òåïåðü ðàáîòà ñ args àíàëîãè÷íà ðàáîòå ñî ñëîâàðåì
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè
Ïàðàìåòðû ïî-óìîë÷àíèþ:
> > def greet ( adr = " mr . " , name = " X " ) :
..
print " Hello " + adr + name + " ! "
..
> > greet ( " mrs . " , " Anderson " )
# Íîðìàëüíûé âûçîâ
Hello mrs . Anderson !
> > greet ( name = " Gates " )
# Èìåíîâàííûé ïàðàìåòð
Hello mr . Gates !
> > greet ()
# Âûçîâ ñ ïàðàìåòðàìè ïî - óìîë÷àíèþ
Hello mr .X !
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ëÿìáäà-ôóíêöèè
Ëÿìáäû ìîæíî âûçûâàòü ñðàçó ïîñëå îïðåäåëåíèÿ
> > ( lambda x : x * x )( 5 )
25
Ôóíêöèè ìîæíî çàäàâàòü ÷åðåç ëÿìáäû
> > foo = lambda x : x * x
> > print foo ( 7 )
49
Ëÿìáäà-ôóíêöèÿ - íå èìåíîâàííàÿ ôóíêöèÿ
Ôóíêöèè â Python - îáúåêòû ïåðâîãî êëàññà
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Çàìûêàíèÿ
Çàìûêàíèå (àíãë. closure) â ïðîãðàììèðîâàíèè - ïðîöåäóðà,
êîòîðàÿ ññûëàåòñÿ íà ñâîáîäíûå ïåðåìåííûå â ñâîåì
ëåêñè÷åñêîì êîíòåêñòå.
>>
..
..
..
..
..
>>
>>
15
>>
>>
20
def make_adder ( x ) :
def adder ( n ) :
# Çàõâàò ïåðìåííîé " x " èç âíåøíåãî êîíòåêñòà
return x + n
return adder
f = make_adder ( 10 )
print f ( 5 ) # 15
f = make_adder ( 15 )
print f ( 5 ) # 20
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå â Python
Ýëåìåíòû ôóíêöèîíàëüíîãî ïðîãðàììèðîâàíèÿ â Python
Ôóíêöèè âûñøèõ ïîðÿäêîâ
Ñïèñî÷íûå âûðàæåíèÿ
Ðåêóðñèÿ
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè âûñøèõ ïîðÿäêîâ
Ôóíêöèÿ map() ïîçâîëÿåò îáðàáàòûâàòü îäíó èëè íåñêîëüêî
ïîñëåäîâàòåëüíîñòåé ñ ïîìîùüþ çàäàííîé ôóíêöèè:
> > list1 = [7 , 2 , 3 , 10 , 12 ]
> > list2 = [ -1 , 1 , -5 , 4 , 6 ]
> > map ( lambda x , y : x *y , list1 , list2 )
[ -7 , 2 , - 15 , 40 , 72 ]
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè âûñøèõ ïîðÿäêîâ
Ôóíêöèÿ lter() ïîçâîëÿåò ôèëüòðîâàòü çíà÷åíèÿ
ïîñëåäîâàòåëüíîñòè. Â ðåçóëüòèðóþùåì ñïèñêå òîëüêî òå
çíà÷åíèÿ, äëÿ êîòîðûõ çíà÷åíèå ôóíêöèè äëÿ ýëåìåíòà
èñòèííî:
> > list = [ 10 , 4 , 2 , -1 , 6 ]
> > filter ( lambda x : x < 5 , list )
[4 , 2 , - 1 ]
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ôóíêöèè âûñøèõ ïîðÿäêîâ
Äëÿ îðãàíèçàöèè öåïî÷å÷íûõ âû÷èñëåíèé â ñïèñêå ìîæíî
èñïîëüçîâàòü ôóíêöèþ reduce():
> > list = [2 , 3 , 4 , 5 , 6 ]
> > reduce ( lambda res , x : res *x , list , 1 )
720
Âû÷èñëåíèÿ ïðîèñõîäÿò â ñëåäóþùåì ïîðÿäêå:
((((1*2)*3)*4)*5)*6
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ñïèñî÷íûå âûðàæåíèÿ - List comprehension
> > S = [ x * * 2 for x in range ( 10 ) ]
> > V = [ 2 * * i for i in range ( 10 ) ]
> > M = [ x for x in S if x % 2 = = 0 ]
>>
> > print S ; print V ; print M
[0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]
[1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 ]
[0 , 4 , 16 , 36 , 64 ]
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
List comprehension vs map and lter
Lc áûñòðåå, åñëè â map íåîáõîäèìî ïåðåäàâàòü ëÿìáäó:
python - mtimeit - s ' xs = range ( 10 ) ' ' map ( lambda x : x +2 , xs ) '
100000 loops , best of 3 : 4 . 24 usec per loop
python - mtimeit - s ' xs = range ( 10 ) ' '[ x + 2 for x in xs ] '
1000000 loops , best of 3 : 1 . 74 usec per loop
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
functools
functools - ìîäóëü ôóíêöèé âûñøåãî ïîðÿäêà.
@functools.total _ordering (cls)
äåêîðàòîð, ïî îäíîìó èëè áîëåå îïåðàòîðó ñðàâíåíèÿ
ãåíåðèðóåò îñòàëüíûå
@functools.lru _cache(maxsize)
äåêîðàòîð, êåøèðóåò äî maxsize âûçîâîâ ôóíêöèè
functools.partial(func, ∗args, ∗ ∗ keywords)
Ñîçäàåò partial îáúåêò, êîòîðûé âåäåò ñåáÿ êàê func ñ
àðãóìåíòàìè args è keywords. Ïðèìåð:
> > from functools import partial
> > basetwo = partial ( int , base = 2 )
> > basetwo ( ' 10010 ')
18
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Ñïàñèáî çà âíèìàíèå!
Àëåêñàíäð Ìèõàéëîâ
Python: ôóíêöèè, ôóíêöèîíàëüíîå ïðîãðàììèðîâàíèå
Скачать