Python Programming for Biology: Bioinformatics and Beyond


Class and object dictionary



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

Class and object dictionary

Python classes have a special inbuilt attribute called __dict__, which, as its name implies,

is a Python dictionary. This is used to store class attributes, including functions. The key



to  access  an  entry  in  the  dictionary  is  the  name  of  the  attribute  (or  function),  and  the

dictionary value is the value of the corresponding attribute (or a function reference). For

example, if we have:

class AminoAcid:

massDict = { … }

acceptableCodes = set(massDict.keys())

then  AminoAcid.__dict__  would  have  an  entry  with  key  ‘massDict’  and  one  with  key

‘acceptableCodes’. Note that the dictionary keys are Python strings.

Instances of objects also have an attribute called __dict__ , and this is used to store the

object’s own attributes. For example, if we have a class definition:

class Molecule:

def __init__(self, name):

# …

self.name = name



and we create a corresponding object via:

molecule = Molecule('myMoleculeName')

then molecule.__dict__ has an entry with key ‘name’ and value ‘myMoleculeName’.

When  Python  needs  to  access  an  attribute  for  an  object  it  first  checks  in  the  object

__dict__ for a key equal to the attribute name, and if that does not exist it then checks in

the associated class __dict__. This can be used to set a default value using a class attribute

that  can  then  optionally  be  overridden  by  an  object  attribute  of  the  same  name.  Class

attributes  take  up  less  memory  than  an  equivalent  object  attribute  because  the  latter  has

space allocated in the object __dict__ for every single object instance of the class.

Python’s  inbuilt  dir()  function,  which  lists  available  attributes  for  a  given  object  or

class, is related to the __dict__ attribute. Acting on a class it gives a list of the keys in the

class __dict__ and acting on an object it returns a merger of the keys for the class __dict__

and for the object __dict__:

dir(Molecule)

# list that includes ['__init__', 'getName', 'getCapitalisedName']

Although it is not a common practice, a class attribute or object attribute (including a

function)  can  be  removed  completely  using  a  del  statement.  Naturally,  class  attributes

must use the del on the class and object attributes use the del on an object:

del AminoAcid.massDict # removes massDict from class

del molecule.name # removes name from molecule

del aminoAcid.massDict # AttributeError exception

del Molecule.name # AttributeError exception

In effect, the del operation is working on the corresponding __dict__:

del molecule.name # same as: del molecule.__dict__['name']





Download 7,75 Mb.

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