Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet69/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   65   66   67   68   69   70   71   72   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Further considerations

Nesting functions

Python allows functions to be defined inside functions; one function is said to be nested

inside the other. Typically you would do this only if the inner function is only of use in the

very  limited  context  of  the  outer  function.  As  with  variables,  this  is  one  way  to  prevent

functions from being used in another module. However, it can make code harder to read,

so it is generally best to use this technique sparingly.

Nonetheless,  sometimes  a  function  inside  a  function  makes  sense.  For  example,  in

Python, lists can be sorted with the sort() function, and it has an optional argument which

is  a  function  that  determines  the  sort  order  (otherwise  the  ‘natural’  sort  order  is  used).

Suppose we have a function that draws atoms, and we want to draw atoms with smaller ‘z’

coordinate first (assuming that ‘z’ is the direction out of the screen). Then we could do

def drawAtoms(atoms):

def atomKey(atom):

return atom.z




atoms.sort(key=atomKey)

Here  the  sort()  function  takes  an  argument,  key  (allowed  since  Python  2.4),  that

specifies a function which takes as argument an item of the list and returns a value which

will be used to compare different items in the list. Here the value returned is the z value of

the atom, so sorting will put atoms with smaller z values first.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   65   66   67   68   69   70   71   72   ...   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