Python Programming for Biology: Bioinformatics and Beyond



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

Class information

Normally  when  an  object  is  manipulated  it  is  just  the  attributes  and  functions  associated

with  it  that  are  of  interest.  However,  sometimes  the  class  itself  is  of  interest.  There  is  a

special object attribute called __class__

7

that gives a handle on the class definition of an



object, although it should be noted that this exists only for user-defined classes:

molecule.__class__ # same as: Molecule

It might not seem obvious why you would need this, given that the class definition was

required to make the object in the first place. Nevertheless, it is surprisingly useful in more

complex situations where you are manipulating objects from a number of different classes,

and you need to check that the right thing happens to the right kind of object.

The  class  definition  itself  has  a  special  attribute  called  __name__  that  gives  the  class

name as a string:

Molecule.__name__ # 'Molecule'

These two special attributes do not appear in the internal attribute dictionary __dict__.

Classes  have  another  special  attribute  called  __doc__,  which  does  appear  in  the

__dict__.  This  is  used  for  adding  textual  comments  to  help  document  the  class,  in  the

same way that the corresponding attribute (also named __doc__) is also used to document

a  function.  It  is  filled  in  from  the  triple-quoted  Python  string  that  appears  immediately

after the class definition, if such a string exists:

class Molecule:

'''This class describes a biological molecule'''

# …


Molecule.__doc__ # 'This class describes a biological molecule'

As  well  as  attributes  accessed  from  objects  and  classes,  there  are  also  inbuilt  Python

functions  that  operate  on  classes  and  objects  to  say  something  about  them.  There  is  a

function, isinstance(), that checks whether a specified object is an instance of a class (was

made with that definition). The class might not be the direct class of the object but could

also be a superclass:

molecule = Molecule('moleculeName')

isinstance(molecule, Molecule) # True

isinstance(molecule, Protein) # False

molecule = Protein('proteinName', 'QWERTY')

isinstance(molecule, Molecule) # True

isinstance(molecule, Protein) # True

This  can  be  useful  when  you  have  objects  that  derive  from  a  known  class,  but  which

include a mixture of subclass and superclass versions, where you only want to operate in a

given way if it is actually a member of the subclass, with its extra bits:

if isinstance(molecule, Protein):

aminoAcids = molecule.aminoAcids # exists since it's Protein



# …

There  is  an  associated  function,  issubclass(),  that  works  on  two  classes  instead  of  an

object and a class:

issubclass(Molecule, Molecule) # True

issubclass(Molecule, Protein) # False

issubclass(Protein, Molecule) # True

issubclass(Protein, Protein) # True

Related to detecting which kind of object you have, there is another concept called the



type of an object. The type of an object is given by the inbuilt type() function. In Python 3

the  type  of  an  object  gives  its  class,  and  the  same  is  true  in  Python  2  for  ‘new-style’

classes, which inherit from object. In Python 2 all user-defined ‘old-style’ classes have the

same type.

type(3) # in v3, in v2

type(3.14) # in v3, in v2

type('red') # in v3, in v2

type(()) # in v3, in v2

type([]) # in v3, in v2

type(set()) # in v3, in v2

type({}) # in v3, in v2

type(molecule) # in v3

# and in v2, new style class

# in v2, old style class

The type() function actually gives you back another Python object, and the type of such

an object is the seemingly odd in Python 3, or in Python 2.

The type objects can also be obtained otherwise:

int # in v3, in v2

float # in v3, in v2

str # in v3, in v2

tuple # in v3, in v2

list # in v3, in v2

set # in v3, in v2

dict # in v3, in v2

Molecule # in v3 and v2

In  Python  2  for  ‘old  style’  classes,  to  get  at  the  type  of  Molecule  without  using  the

type() function we need to import the types module and then we have:

import types

types.InstanceType # in v2 old style class

Checking the object type comes in handy when some variable could have one of a few

types, and how it gets processed depends on the type. For example, a function might have

an argument that could be a tuple or a dictionary, where the following code could be used:

def f(x):

if isinstance(x, tuple):

# do something

elif isinstance(x, dict):




# do something else

else:


# error: raise an exception


Download 7,75 Mb.

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