Using import

Python kutubxonalarni import qilishning bir necha usuliga ega.

SU

importdan foydalanish

Python kutubxonalarni import qilishning bir necha usuliga ega. Eng oddiy va mashhur usul — bu import kalit so‘zini kutubxona nomi bilan birga ishlatishdir. Importlar odatda Python faylingiz yoki skriptingizning yuqori qismida joylashtirilishi kerak, shunda dasturingizdagi barcha kod kutubxonaga kirish huquqiga ega bo‘ladi.

Keling, bir misolga nazar tashlaymiz:

>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__',
 '__loader__', '__name__', '__package__', '__spec__', '__stderr__',
 '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames',
 '_debugmallocstats', '_getframe', '_git', '_home', '_xoptions',
 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix',
 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats',
 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info',
 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags',
 'float_info', 'float_repr_style', 'get_asyncgen_hooks',
 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval',
 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors',
 'getfilesystemencoding', 'getprofile', 'getrecursionlimit',
 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info',
 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing',
 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks',
 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2',
 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval',
 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval',
 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version',
 'version_info', 'warnoptions']

Bu yerda nima sodir bo‘ldi? Siz import sys deb yozganingizda, Pythonning tizim (sys) modulini import qildingiz, bu modul Python skriptiga qanday argumentlar uzatilganligi, audit hook qo‘shish va boshqa vazifalarni bajarishga yordam beradi. Batafsil ma'lumotni bu yerda o‘qishingiz mumkin:

Siz Pythonning dir() buyrug‘idan foydalanib, sys kutubxonasida sizga mavjud bo‘lgan narsalarni ko‘rdingiz. Bu Python’da introspektsiya deb ataladi va siz bu haqda 19-bobda ko‘proq bilib olasiz.

Yana bir qiziqarli importlardan biri Pythonning yashirin “Easter egg”laridan biri:

>>> import this
The Zen of Python, by Tim Peters
 
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Ushbu kodni ishlatsangiz, Pythonning “Zen” qoidalari chop etiladi — bu Pythonni "eng yaxshi" yozish usullarini tasvirlaydigan "qoidalar" to‘plamidir.

Shuningdek, bir nechta kutubxonalarni bitta qatorda import qilish mumkin:

>>> import math, os

Bu odatda tavsiya etilmaydi, lekin albatta ruxsat etilgan. Agar siz Pythonning uslubiy qo‘llanmasi PEP8-ga rioya qilmoqchi bo‘lsangiz, bunday qilmasligingiz kerak. Ammo agar tashkilotingiz buni qabul qilsa, bu sizning ixtiyoringizda. Faqat izchillikni saqlang!

Bir marta kutubxonani import qilganingizdan so‘ng, uning funksiyalari va sinflarini chaqirishingiz mumkin. Masalan:

>>> import math
>>> math.sqrt(4)
2.0

Bu yerda siz math modulidagi sqrt() funksiyasini chaqirib, 4 ning kvadrat ildizini oldingiz.

Ba'zida siz faqat ma'lum qismlarni import qilishni xohlaysiz. Keyingi qismda buni qanday qilishni o‘rganamiz!

Ushbu sahifada

Xato haqida xabar berish