Python Programming for Biology: Bioinformatics and Beyond


Lambda: anonymous functions



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

Lambda: anonymous functions

So  far  we  have  been  discussing  the  normal  named  Python  functions.  However,  Python

also has some seemingly strange functionality to define anonymous functions. Here’s how

you  can  define  an  anonymous  function  using  the  lambda  keyword;  note  how  it  is

immediately assigned to a variable and that a colon ‘:’ is used to separate the inputs from

what is returned.

cube = lambda x: x*x*x

print(cube(3)) # Result: 27

This is equivalent to:

def cube(x):

return x*x*x

print(cube(3)) # Result: 27

A  lambda  function  can  only  be  simple;  it  cannot  contain  control  statements  like  if  or

for. So its use is restricted. There might seem little point in defining such limited functions

that you cannot name, but there are a few situations where they are very handy. Consider

that  you  have  a  function  that  accepts  another  function  as  an  argument;  in  this  case  the

argument function is called if there is an error:

def jobFunc(arg1, errorFunc):

Now imagine that you want to pass some arguments to errorFunc to say how the error

should be displayed:

def jobFunc(arg1, errorFunc('Warning', color='Red')): # Wrong

The  above  is  not  what  we  want  because  errorFunc  is  always  called  when  jobFunc  is

defined.  What  we  really  want  to  do  is  only  call  errorFunc  with  the  warning  message

somewhere inside jobFunc, when and where it is needed. Accordingly we can use lambda

to wrap errorFunc without it being called.

def jobFunc(arg1, lambda: errorFunc('Warning', color='Red')):

As  another  demonstration,  consider  the  sorting  example  for  nested  functions  in  the

previous section. We could use lambda instead:

def drawAtoms(atoms):

atoms.sort(key=lambda atom: atom.z)





Download 7,75 Mb.

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