Birçok kişi importvs hakkında zaten açıklamış olsa da import from, kaputun altında neler olduğu ve değiştiği tüm yerlerin nerede olduğu hakkında biraz daha açıklamak istiyorum.
import foo:
fooGeçerli ad alanındaki bu modüle içe aktarılır ve bir başvuru oluşturur. Ardından, modülün içinden belirli bir özniteliğe veya yönteme erişmek için tamamlanmış modül yolunu tanımlamanız gerekir.
Örneğin foo.barama değilbar
from foo import bar:
fooListelenen tüm üyeleri ( bar) içe aktarır ve referanslar oluşturur . Değişkeni ayarlamaz foo.
Örneğin barama değil bazveyafoo.baz
from foo import *:
İthalat foove geçerli ad alanında bu modül tarafından tanımlanan tüm kamu nesnelere başvuru oluşturur (listelenen her şey __all__eğer __all__mevcutsa, ile başlamaz aksi takdirde her şey _). Değişkeni ayarlamaz foo.
Örneğin bar, bazama değil _quxya da foo._qux.
Şimdi ne zaman yaptığımızı görelim import X.Y:
>>> import sys
>>> import os.path
Kontrol sys.modulesadıyla osve os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Kontrol globals()ve locals()ad alanı dikte osve ile os.path:
>>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>
Yukarıdaki örnekte, yalnızca osyerel ve global ad alanına eklendiğini tespit ettik . Bu yüzden şunları kullanabilmeliyiz:
>>> os
<module 'os' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> os.path
<module 'posixpath' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
Ama değil path.
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Silmek kez oshalktan () ad, erişmeye mümkün olmayacaktır ossıra sıra os.pathonlar sys.modules de bulunmasına rağmen:
>>> del locals()['os']
>>> os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Şimdi konuşalım import from:
from:
>>> import sys
>>> from os import path
Kontrol sys.modulesile osve os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Biz sys.modulesdaha önce kullanarak yaptığımız gibi bulduğumuz bulunduimport name
Tamam, nasıl göründüğünü kontrol edelim locals()ve globals()ad alanı dikte ediyor:
>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>
Aşağıdakileri pathdeğil, adı kullanarak erişebilirsiniz os.path:
>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
'Yolu' şuradan silelim locals():
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Takma ad kullanan son bir örnek:
>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
Ve tanımlanmış bir yol yok:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>