• No results found

efining a function

In document python for biologists (Page 109-112)

+ets go ahea% an% !reate o&r get_at_content f&n!tion. (efore e start typing,  e nee% to fig&re o&t hat the inp&ts the n&mber an% types of the function

arguments= an% o&tp&ts the type of the return value= are going to be. For this f&n!tion, that seems pretty obvio&s 5 the inp&t is going to be a singe D;4

se<&en!e, an% the o&tp&t is going to be a %e!ima n&mber. $o transate these into #ython terms: the f&n!tion i ta-e a singe arg&ment of type string , an% i ret&rn a va&e of type number 1. 7eres the !o%e:

def get/at/content(dna#! length B len(dna#

a/count B dna.count(A# t/count B dna.count()#

at/content B (a/count C t/count#  length return at/content

Reminder: if yo&re &sing #ython 2 rather than #ython 3, in!&%e this ine at the top of yo&r program:

from //future// import diision

$he first ine of the f&n!tion %efinition !ontains a severa %ifferent eements. e start ith the or% def, hi!h is short for "efine riting a f&n!tion is !ae% "efining  it=. Fooing that e rite the name of the f&n!tion, fooe% by the names of the arg&ment variabes in parentheses. J&st i-e e sa before ith

1 'n fa!t, e !an be a itte bit more spe!ifi!: e !an say that the ret&rn va&e i be of type float 5 a foating point n&mber i.e. one ith a %e!ima point=.

1

10011 CChhaapptteer r : : rriittiinng g oo&&r r oon n ff&&nn!!ttiioonnss

norma variabes, the f&n!tion name an% the

norma variabes, the f&n!tion name an% the arg&ment names are arg&ment names are arbitrary 5 arbitrary 5 ee !an &se hatever e i-e.

!an &se hatever e i-e. $he first ine en%s

$he first ine en%s ith a !oon, 9&st i-ith a !oon, 9&st i-e the first e the first ine of the oops ine of the oops that e erethat e ere oo-ing at in the previo&s !hapter. 4n% 9&st i-e oops, this ine is fooe% by a oo-ing at in the previo&s !hapter. 4n% 9&st i-e oops, this ine is fooe% by a block

block of in%ente%  of in%ente% ines 5 theines 5 the function bo"y  function bo"y . $he f&n!tion bo%y !an have as . $he f&n!tion bo%y !an have as manymany ines of !o%e as

ines of !o%e as e i-e, as ong as they a e i-e, as ong as they a have the same have the same in%entation. in%entation. ithin theithin the f&n!tion bo%y

f&n!tion bo%y, e !an refer to the arg&ments by &sing the variabe names , e !an refer to the arg&ments by &sing the variabe names from thefrom the first ine. 'n this

first ine. 'n this !ase, the variabe!ase, the variabe dnadna refers to the se<&en!e that as passe% in as refers to the se<&en!e that as passe% in as the arg&ment to

the arg&ment to the f&n!tion.the f&n!tion. $he ast ine of

$he ast ine of the f&n!tion !a&ses it to ret&rn the 4$ !onthe f&n!tion !a&ses it to ret&rn the 4$ !ontent that as !a!&ate%tent that as !a!&ate% in the f&n!tion bo%y. $o

in the f&n!tion bo%y. $o returnreturn from a  from a f&n!tion, f&n!tion, e simpy rite e simpy rite ret&rn fooe%ret&rn fooe% by the va&e that

by the va&e that the f&n!tion sho&% o&tp&t.the f&n!tion sho&% o&tp&t. $here are a

$here are a !o&pe of important things to be aare of hen riting f&n!tions.!o&pe of important things to be aare of hen riting f&n!tions. Firsty,

Firsty, e nee% to e nee% to ma-e a !ear ma-e a !ear %istin!tion beteen%istin!tion beteen "efining "efining  a f&n!tion, an% a f&n!tion, an% running 

running  it e refer to r&nning a f&n!tion as it e refer to r&nning a f&n!tion as calling calling it=. it=. $he !o%e $he !o%e eve eve rittenritten

above i not !a&se anything to happen hen e r&n it, be!a&se eve not a!t&ay  above i not !a&se anything to happen hen e r&n it, be!a&se eve not a!t&ay  as-e% #ython to ee!&te the

as-e% #ython to ee!&te the get_at_contentget_at_content f&n!tion 5 e have simpy %efine% f&n!tion 5 e have simpy %efine%  hat it is.

 hat it is. $he !o%e in the f&n!$he !o%e in the f&n!tion i notion i not be ee!&te% &nt be ee!&te% &nti e !a the f&n!ti e !a the f&n!tiontion i-e this:

i-e this:

get/at/content(*A)%A&)%%A&&A*# get/at/content(*A)%A&)%%A&&A*#

'f e simpy !a the f&n!tion i-e that, hoever, then the 4$ !ontent i vanish 'f e simpy !a the f&n!tion i-e that, hoever, then the 4$ !ontent i vanish on!e it

on!e its been s been !a!&ate%. '!a!&ate%. 'n or%er to n or%er to &se the &se the f&n!tion to %o something &sef&, ef&n!tion to %o something &sef&, e m&st either store the res&t in a variabe:

m&st either store the res&t in a variabe:

at/content B get/at/content(*A)%A&)%%A&&A*# at/content B get/at/content(*A)%A&)%%A&&A*#

?r &se it %ire!ty: ?r &se it %ire!ty:

1

10022 CChhaapptteer r : : rriittiinng g oo&&r r oon n ff&&nn!!ttiioonnss

print(*A) content is * C str(get/at/content(*A)%A&)%%A&&A*### print(*A) content is * C str(get/at/content(*A)%A&)%%A&&A*###

"e!on%y, i

"e!on%y, its important to &n%erstan% that the ts important to &n%erstan% that the arg&ment variabearg&ment variabe dnadna %oes not %oes not ho% any parti!&ar va&e hen the f&n!tion is %efine%

ho% any parti!&ar va&e hen the f&n!tion is %efine%11. 'nstea%, its 9ob is to ho%. 'nstea%, its 9ob is to ho%  hatever va

 hatever va&e is given as the arg&ment &e is given as the arg&ment hen the f&n!tion is !ahen the f&n!tion is !ae%. 'n e%. 'n this ay itthis ay itss anaogo&s to the oop variabes e sa in the pr

anaogo&s to the oop variabes e sa in the previo&s !hapter: oop variabes ho%evio&s !hapter: oop variabes ho% a %ifferent va&e ea!h time ro&n% the

a %ifferent va&e ea!h time ro&n% the oop, anoop, an% f&n!tion arg&ment variabes ho% a% f&n!tion arg&ment variabes ho% a %ifferent va&e ea!h time

%ifferent va&e ea!h time the f&n!tion is !ae%.the f&n!tion is !ae%. Finay

Finay, be aare that the same s!oping r&es that appie% t, be aare that the same s!oping r&es that appie% to oops aso appy too oops aso appy to f&n!tions 5

f&n!tions 5 any variabes that e !reate as part of any variabes that e !reate as part of the f&n!tion ony eist insi%e thethe f&n!tion ony eist insi%e the f&n!tion, an

f&n!tion, an% !annot be a!!esse% o&tsi%e. 'f e tr% !annot be a!!esse% o&tsi%e. 'f e try to &se y to &se a variabe thats !reate%a variabe thats !reate% insi%e the f&n!tion from o&tsi%e:

insi%e the f&n!tion from o&tsi%e:

def get/at/content(dna#! def get/at/content(dna#! length B len(dna# length B len(dna# a/count B dna.count(A# a/count B dna.count(A# t/count B dna.count()# t/count B dna.count()#

at/content B (a/count C t/count#  length at/content B (a/count C t/count#  length return at/content

return at/content print(a/count#

print(a/count#

e get an error: e get an error:

<ame9rror! name a/count is not defined <ame9rror! name a/count is not defined

1

1

10033 CChhaapptteer r : : rriittiinng g oo&&r r oon n ff&&nn!!ttiioonnss

In document python for biologists (Page 109-112)