Python Programming for Biology: Bioinformatics and Beyond



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

Variable scope

Next  we  move  on  to  consider  the  scope  of  variables  inside  and  outside  functions.  The

scope  of  a  variable  is  the  context  within  a  program  where  the  variable’s  name  has

meaning. It is often convenient to think about this in terms of whether a variable operates

locally or not; i.e. whether a variable only exists inside a block of code.

Module and function variables

If a variable is defined outside a function then its name has meaning for the whole Python

file or module in which it resides. Such a variable is usually visible to other modules, so

can be imported from one module to another, unless it is specifically protected from being

exported by having a name beginning with double underscore ‘__’. If a variable is defined

inside a function then it is visible only inside that function.

def mathFunction(x, y):

z = (x+y)*(x-y)

return z

answer = mathFunction(4, 7)

print(answer) # Fine

print(z) # Fails; z does not exist outside function definition

In  general  most  Python  code  will  be  defined  as  functions  and  thus  most  variables

should  be  defined  inside  those  functions.  Keeping  variables  inside  functions,  and  thus

their  scope  local,  has  several  distinct  advantages:  the  value  of  the  variable  cannot  be

altered outside the function, so there is no ‘pollution’ from elsewhere; you don’t have to

worry whether your variables are being used by other bits of code

1

and the logic is usually



more obvious, since it is contained.

An occasion where a variable might be defined outside a function is when it is intended

to  be  a  constant,  so  not  modified  after  its  original  definition.  For  example,  consider  the

following two functions to convert masses between units of pounds and kilograms:

KILOS_PER_POUND = 0.45359237

def poundsToKilos(pounds):

kilos = pounds * KILOS_PER_POUND

return kilos

def kilosToPounds(kilos):

pounds = kilos / KILOS_PER_POUND

return pounds

The  constant  POUNDS_PER_KILO  could  of  course  also  be  defined  inside  the

functions,  but  it  is  useful  to  both,  and  does  not  change.  Thus  in  this  case  it  makes  more

sense to define the constant at the module level, outside the functions.





Download 7,75 Mb.

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