Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet134/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   130   131   132   133   134   135   136   137   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Miscellaneous

Lastly, we move on to the last few tips that don’t seem to fit in the other categories.

To write some test code in Python that is not run when the file is imported as a module,

but which is run when the file is used directly, the __name__ attribute for the module can

be  inspected.  This  attribute  belongs  to  the  local  Python  file  and  is  only  equal  to  the  text



string ‘__main__’ when called directly.

if __name__ == '__main__':

testSomething()

When  performing  imports  it  is  good  practice  to  issue  them  at  the  top  of  a  file  where

possible,  so  you  can  see  at  a  glance  what  is  being  used.  Also,  doing  imports  outside

functions  is  generally  quicker.  However,  imports  inside  function  definitions  may  be

needed to avoid circular imports (i.e. a imports b which imports a which imports b …). So

avoid:


def func(x, y):

from SomeModule import someFunction

someFunction(c)

when you can do:

from SomeModule import someFunction

def func(x, y):

someFunction(x)

Importing  everything  from  a  module  with  the  ‘*’  notation  slows  down  execution

(including because it interferes with the optimisation of the Python compiler). It may also

lead to other problems:

from library import * # Generally bad

from somethingElse import * # Even worse to do it twice in one module

The  problem  is  that  you  may  not  be  sure  what  you’ve  imported,  and  you  might  have

imported something with a name that clashes with one of the internal names or one of the

names  from  another  import.  Also,  if  you  do  this  for  multiple  modules  it  won’t  be  at  all

clear which thing comes from which module. If you need lots from the imported modules

it is better to consider using dot notation, if you don’t want to make imports explicit. So,

for example:

import library

import somethingElse

x = library.magic()

y = somethingElse.magic()

If you need to know what attributes and methods an object has, try the dir() keyword:

dir(obj)


Or maybe try the pydoc graphical interface and have a browse:

pydoc -g # Python 2

pydoc3 -b # Python 3


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   130   131   132   133   134   135   136   137   ...   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