Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet90/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   86   87   88   89   90   91   92   93   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Class definition

The  definition  of  a  class  specification  is  begun  using  the  class  keyword,  followed  by  a

name used to identify the class:

class Molecule:

# contents of the implementation

The  implementation  code,  which  defines  how  the  class  is  constructed  and  operates,  is

indented  relative  to  the  start  of  the  class  keyword,  in  keeping  with  the  style  of  other

separated  code  blocks  elsewhere  in  Python.  In  particular,  functions  are  usually  defined

within the indented code block of a class, such that these functions ‘belong’ to that class

definition.

For defining a subclass, which is based on the definition of some other class, we have to

include the superclass in parentheses:

class Protein(Molecule):

# extra contents for subclass implementation

Unlike  many  computer  languages  (e.g.  Java),  Python  allows  multiple  inheritance,

which is to say that a class can have more than one superclass, and inherit properties from

all  of  these.  In  this  case  the  names  of  the  superclasses  are  listed,  separated  by  commas,

within  the  parentheses.  Multiple  inheritance,  however,  is  generally  rare;  it  is  much  more

common  to  have  only  one  superclass,  like  the  Protein  above,  or  no  superclass  at  all  like

Molecule.

In  Python  2  there  is  a  slight  complication  with  Python  classes,  which  stems  from  the

fact that ‘new-style’ classes were introduced, for technical reasons.

1

In most circumstances



you  can  define  things  in  the  ‘classic’  way  as  we  have  illustrated,  but  if  you  are  using  a


version of Python from 2.2 to 2.7 you can use the new style by making the class definition

a subclass of the basal Python class, which uses the keyword object.

2

class Molecule(object): # New style



# contents of the implementation

In Python 3 all classes inherit from object and you do not have to specify this explicitly

(although you can).

When writing your own class code, it is common practice, but certainly not mandatory,

to save each class definition within a single, dedicated file on disk and to call that file after

the name of the class. For example, we might have a class called Molecule implemented in

a file named ‘Molecule.py’ and Protein implemented in one named ‘Protein.py’. Generally

this  goes  along  with  the  notion  that  your  class  definitions  may  be  useful  in  different

situations,  whereupon  each  definition  can  be  imported,  to  be  used  within  any  Python

program  as  needed.  To  access  such  classes,  saved  in  other  files  (modules),  one  has  to

import the class, for example, here via:

from Molecule import Molecule

and

from Protein import Protein



Above, the first name in each line is the name of the module, which relates to the name

of  the  file  (‘Molecule.py’  or  ‘Protein.py’)  from  which  to  load  data.  Note  that  the  ‘.py’

suffix of the file name is not included. The second name, after the import statement, is the

name of the class (Molecule, Protein). It is only our convention that the module names and

class names are the same, but it is a common one which you should be familiar with.

There is an alternative approach, especially for closely related classes, where multiple

class definitions are contained in a single file. For example, we could decide to put both

Molecule and Protein into the same file, here somewhat arbitrarily called ‘Molecules.py’.

In  this  case  the  import,  assuming  you  need  access  to  both  classes  in  your  code,  would

instead look like:

from Molecules import Molecule, Protein

How exactly object class definitions are arranged, in terms of which module they reside

in and what the module names are, are both matters of taste and convenience.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   86   87   88   89   90   91   92   93   ...   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