Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet51/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   47   48   49   50   51   52   53   54   ...   183
Bog'liq
python3-oop

[
 42 
]
We can use more periods to go further up the hierarchy. Of course, we can also 
go down one side and back up the other. We don't have a deep enough example 
hierarchy to illustrate this properly, but the following would be a valid import if
we had an 
ecommerce.contact
package containing an 
email
module and wanted
to import the 
send_mail
function into our 
paypal
module:
from ..contact.email import send_mail
This import uses two periods to say, 
the parent of the payments package
, and then uses 
the normal 
package.module
syntax to go back 
up
into the contact package.
Finally, we can import code directly from packages, as opposed to just modules inside 
packages. In this example, we have an 
ecommerce
package containing two modules 
named 
database.py
and 
products.py
. The database module contains a 
db
variable 
that is accessed from a lot of places. Wouldn't it be convenient if this could be imported 
as 
import ecommerce.db
instead of 
import ecommerce.database.db
?
Remember the 
__init__.py
file that defines a directory as a package? This file can 
contain any variable or class declarations we like, and they will be available as part of 
the package. In our example, if the 
ecommerce/__init__.py
file contained this line:
from .database import db
We can then access the 
db
attribute from 
main.py
or any other file using this import:
from ecommerce import db
It might help to think of the 
__init__.py
file as if it was an 
ecommerce.py
file if that 
file were a module instead of a package. This can also be useful if you put all your 
code in a single module and later decide to break it up into a package of modules. 
The 
__init__.py
file for the new package can still be the main point of contact for 
other modules talking to it, but the code can be internally organized into several 
different modules or subpackages.
I recommend not putting all your code in an 
__init__.py
file, though. Programmers 
do not expect actual logic to happen in this file, and much like with 
from x import 
*
, it can trip them up if they are looking for the declaration of a particular piece of 
code and can't find it until they check 
__init__.py
.
www.it-ebooks.info


Chapter 2

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   47   48   49   50   51   52   53   54   ...   183




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