Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet132/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   128   129   130   131   132   133   134   135   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Functions

For  keeping  code  tidy,  depending  on  circumstances,  you  can  consider  collecting  a

function’s arguments into a dictionary before it is called:

animal = longFunctionName(name='Benny', age=9, furColor='black',

legs=4, tail=True)

The above can, if it helps, be changed to:

kwds = dict(name='Benny',

age=9,



furColor='black',

legs=4,


tail=True)

x = longFunctionName(**kwds)

If you need to collect function arguments for calling some other function internally, but

don’t want to have to state them explicitly, you can collect them with ‘*’ notation and pass

them on untouched.

def func1(a, b, c, *args):

d = func2(*args)

To  call  a  different  function  depending  upon  a  value  you  could  use  conditional



statements:

if x = 1:

funcA()

elif x = 2:

funcB()

elif x ==3:

funcC()

or perhaps more elegantly do a dictionary look-up:

funcDict = {1:funcA, 2:funcB, 3:funcC}

funcDict[x]()



Memoization is a handy technique if you have a slow operation, but the operation may

be repeated, so you can pass back the result from last time (given the same inputs) rather

than calculating again. In Python we can implement this in a function by using a default

argument such as a dictionary. It sometimes causes trouble if a dictionary or list is used for

a default function argument, because the collection is not automatically renewed each time

the function is called (the same object is always used). However, for memoization we can

exploit this to make a function cache.

def verySlowCalcOnSimpleData(data, cache={}):

key = getCacheKey(data) # This could be as simple as making a tuple

answer = cache.get(key)

if answer is not None: # Assumes the answer is never None

return answer # Quick response

answer = someVerySlowCalculation(data)

cache[key] = answer # Store the result for next time

return answer

Note that the input data itself can sometimes be used as a key to the cache dictionary,

but in general a key may need generating so that it can be used in a dictionary.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   128   129   130   131   132   133   134   135   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish