Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet108/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   104   105   106   107   108   109   110   111   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Properties

Python has a really handy mechanism for allowing the syntax of direct attribute access but

with  the  protection  of  using  getter  and  setter  functions.  This  is  called  a  Python  property

and is made available via the inbuilt property() function. In Python 2 it is only (properly)

available for ‘new-style’ classes; those that inherit from object. In Python 3, all classes are

‘new style’ so they all support this mechanism. It is best to illustrate with an example, so

consider the code:

class Structure(object):

# …

def getName(self):



return self._name

def setName(self, name):

if not name:

raise Exception('name must be set to non-empty string')

self._name = name

name = property(getName, setName)




What  this  means  is  that  use  of  the  name  attribute  automatically  calls  getName()  or

setName(), rather than accessing a simple attribute. For example:

name = structure.name

# equivalent to: name = structure.getName()

structure.name = name

# equivalent to: structure.setName(name)

Note  that  the  information  is  stored  internally  using  the  variable  self._name.  Here  the

variable  could  not  instead  be  self.name  because  that  would  result  in  getName()  or

setName() being called recursively, and so end up with an ‘infinite’ loop. In the property

definition the setter function is optional, and this provides a good way to implement frozen

attributes. As an example, if we suppose chain.molType was frozen we could have:

class Chain(object):

# …

def getMolType(self):



return self._molType

molType = property(getMolType)

In  this  case,  trying  to  assign  the  attribute’s  value  would  give  rise  to  an  AttributeError

exception:

chain.molType = 'DNA' # --> AttributeError exception

The  property()  function  has  a  third  optional  argument  to  specify  a  delete  function,

which is called when the attribute is deleted using ‘del’, and a fourth optional argument to

specify documentation for the property.

From  Python  2.6,  there  is  an  alternative  syntax  for  specifying  a  property,  using

decorators (see

Chapter 5

). Again these only apply to ‘new-style’ classes.

class Structure(object):

# …


@property

def name(self):

return self._name

@name.setter

def name(self, value):

if not value:

raise Exception('value must be set to non-empty string')

self._name = value

This is not any shorter than the non-decorator form, and it also looks odd that both the

get function and the set function are called name.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   104   105   106   107   108   109   110   111   ...   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