Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet96/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   92   93   94   95   96   97   98   99   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

String attribute access

Sometimes it is useful to access an attribute using a string that contains the name of the

attribute,  rather  than  directly  using  the  attribute  itself.  The  following  examples  will

hopefully make the distinction clear. Firstly, there is the inbuilt Python function, hasattr(),

which says whether a given object has a given attribute:

hasattr(molecule, 'name') # True

hasattr(molecule, 'style') # False

Note that the second argument is indeed a string that describes the name of the attribute.

This function also works for class attributes and in that case the first argument can also be

a class rather than an object:

hasattr(AminoAcid, 'massDict') # True

There are two accompanying functions, getattr() and setattr(), also inbuilt. For example,

we can get an attribute value using a string containing the attribute’s name via:

name = getattr(molecule, 'name')

# same as: name = molecule.name

and  we  can  set  an  attribute  value  in  a  similar  manner,  noting  that  we  pass  in  three

arguments, because we need the value to assign:

setattr(molecule, 'name', 'newMoleculeName')

# same as: molecule.name = 'newMoleculeName'

The getattr() and setattr() functions might seem redundant. After all, we would usually

just do:

name = molecule.name

In the above examples that would be a fair point, because the attribute of interest is a

specific  one.  However,  sometimes  in  code  we  more  generally  access  different  attributes

using  a  variable  name,  rather  than  by  directly  referring  to  a  particular  name  known  to

Python, and that is where these additional functions come in very handy. It is not unusual

to see code like the following, which is general for any attribute; in this case a check to

make sure a named attribute is not accessed unless it is defined:

if hasattr(molecule, attr):

value = getattr(molecule, attr)

# …

For example, attr might be ‘aminoAcids’, which in our example code exists if molecule



is a Protein but not if it is just a plain Molecule. In the next section we see a better way to

check if an object is an instance of a specific class.

To  complete  the  suite  of  inbuilt  attribute  functions,  there  is  also  a  delattr()  function,

which is equivalent in functionality to the del statement:

delattr(molecule, 'name') # same as: del molecule.name




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   92   93   94   95   96   97   98   99   ...   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