Bài đăng này thảo luận về Python từ from import *
và from import *
, cách họ cư xử và tại sao nó có thể [là!] Một ý tưởng tồi để sử dụng chúng.
Nhập `*` từ một mô -đun
from import *
có nghĩa là tôi muốn truy cập vào tất cả các tên trong mà tôi có nghĩa là có quyền truy cập vào. Vì vậy, hãy để nói rằng chúng tôi có những điều sau đây
something.py
:
# something.pypublic_variable = 42
_private_variable = 141def public_function[]:
print["I'm a public function! yay!"]def _private_function[]:
print["Ain't nobody accessing me from another module...usually"]class PublicClass[object]:
passclass _WeirdClass[object]:
pass
Trong trình thông dịch Python, chúng ta có thể thực thi
>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
0 và xem những điều sau:>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
Vì vậy,
>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
0 nhập tất cả các tên từ >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
2 khác với các tên bắt đầu bằng một >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
3; bởi vì họ thường có nghĩa là riêng tư.Ermm, mà không quá tệ! Đó là `__all__`?
Những gì mà không đề cập ở trên là
>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 là gì. >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 là danh sách các chuỗi xác định các ký hiệu nào trong mô -đun [hoặc gói như chúng ta sẽ thấy sau] sẽ được xuất khi from import *
được sử dụng trên mô -đun. Nếu chúng tôi không xác định >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 [chúng tôi đã không ở something.py
ở trên], thì hành vi mặc định của >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
9 là nhập tất cả các tên ngoại trừ các tên bắt đầu bằng dấu gạch dưới [>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
3]. Một lần nữa, vì thông thường, một dấu gạch dưới được sử dụng để nói một biểu tượng là riêng tư, tất cả đều có ý nghĩa. Hãy để xem những gì xảy ra khi chúng ta xác định >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 của chính mình trong something.py
:# something.py__all__ = ['_private_variable', 'PublicClass']# The rest is the same as beforepublic_variable = 42
_private_variable = 141def public_function[]:
print["I'm a public function! yay!"]def _private_function[]:
print["Ain't nobody accessing me from another module...usually"]class PublicClass[object]:
passclass _WeirdClass[object]:
pass
Bây giờ, chúng tôi hy vọng
>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
0 sẽ chỉ nhập tên # something.py__all__ = ['_private_variable', 'PublicClass']# The rest is the same as beforepublic_variable = 424 và
_private_variable = 141def public_function[]:
print["I'm a public function! yay!"]def _private_function[]:
print["Ain't nobody accessing me from another module...usually"]class PublicClass[object]:
passclass _WeirdClass[object]:
pass
# something.py__all__ = ['_private_variable', 'PublicClass']# The rest is the same as beforepublic_variable = 425:
_private_variable = 141def public_function[]:
print["I'm a public function! yay!"]def _private_function[]:
print["Ain't nobody accessing me from another module...usually"]class PublicClass[object]:
passclass _WeirdClass[object]:
pass
>>> from something import *
>>> public_variable
...
NameError: name 'public_variable' is not defined
>>> _private_variable
0
>>> public_function[]
...
NameError: name 'public_function' is not defined
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
Còn gói thì sao?
Khi nhập * từ một gói,
>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 thực hiện điều tương tự như đối với các mô -đun, ngoại trừ nó liên quan đến các mô -đun trong gói [trái ngược với việc chỉ định tên trong mô -đun]. Vì vậy, >>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 chỉ định tất cả các mô -đun sẽ được tải và nhập vào không gian tên hiện tại khi chúng tôi sử dụng from import *
.Tuy nhiên, sự khác biệt là, khi bạn bỏ qua tuyên bố
>>> from something import *
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 trong gói ____ ____30, thì tuyên bố from import *
sẽ không nhập bất cứ điều gì [không hoàn toàn đúng; đọc điều này cho sự thật].Nhưng, tại sao điều này là xấu?
Trước khi tiếp tục, trong phiên dịch viên Python của bạn,
>>> from something import *
>>> public_variable
...
NameError: name 'public_variable' is not defined
>>> _private_variable
0
>>> public_function[]
...
NameError: name 'public_function' is not defined
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
2 và đọc lại Zen of Python [đọc nó cho con bạn mỗi đêm trước khi chúng ngủ].Rõ ràng là tốt hơn tiềm ẩn.
from import *
không rõ ràng. Nó không phải là người cho chúng tôi biết bất cứ điều gì về những gì chúng tôi đang nhập khẩu hoặc những cái tên mà chúng tôi mang vào không gian tên của chúng tôi. Sẽ tốt hơn nhiều để chỉ định và nhập mọi thứ một cách rõ ràng chúng ta cần; Bằng cách đó, một người đọc [rất có thể là tương lai của bạn] đã giành được sự nhầm lẫn về nơi một biến/chức năng/lớp/vv. Được sử dụng trong mã đến từ, dẫn chúng ta đến:
Tính dễ đọc.
Ngay cả khi cần có rất nhiều tên, nó vẫn rõ ràng hơn nhiều khi nhập từng cái một, rõ ràng. Sử dụng PEP 328:
from Tkinter import [Tk, Frame, Button, Entry, Canvas, Text,
LEFT, DISABLED, NORMAL, RIDGE, END]
Bây giờ bạn đã biết chính xác những gì bạn có trong không gian tên của bạn và một
>>> from something import *
>>> public_variable
...
NameError: name 'public_variable' is not defined
>>> _private_variable
0
>>> public_function[]
...
NameError: name 'public_function' is not defined
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
4 nhanh [nói theo nghĩa bóng] có thể cho bạn biết một cái tên đến từ đâu.Ngoài ra, bạn luôn gặp rủi ro nếu/khi mô -đun/tác giả gói quyết định thay đổi nội dung của danh sách [thêm/xóa nội dung vào/từ nó]. Sau đó, một trong hai
- Tác giả đã xóa một chuỗi từ
>>> from something import *
4. Nếu mã của bạn đang sử dụng tên đó, mã của bạn sẽ tăng
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined>>> from something import *
6 và nó sẽ khó tìm hiểu lý do tại sao.
>>> public_variable
...
NameError: name 'public_variable' is not defined
>>> _private_variable
0
>>> public_function[]
...
NameError: name 'public_function' is not defined
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined - Tác giả đã thêm [nhiều] chuỗi mới vào
>>> from something import *
4. Bạn có thể không cần tất cả/bất kỳ tên mới nào và bạn chỉ cần tạo ra không gian tên của bạn với những thứ bạn không quan tâm. Họ thậm chí có thể theo dõi một số tên khác mà bạn không nhận ra.
>>> public_variable
42
>>> _private_variable
...
NameError: name '_private_variable' is not defined
>>> public_function[]
"I'm a public function! yay!"
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
Tất nhiên, đôi khi có thể hữu ích hoặc cần thiết để nhập
>>> from something import *
>>> public_variable
...
NameError: name 'public_variable' is not defined
>>> _private_variable
0
>>> public_function[]
...
NameError: name 'public_function' is not defined
>>> _private_function[]
...
NameError: name '_private_function' is not defined
>>> c = PublicClass[]
>>> c
>>> c = _WeirdClass[]
...
NameError: name '_WeirdClass' is not defined
8 từ các mô -đun và/hoặc gói. Tuy nhiên, nó tốt nhất để đảm bảo bạn thực sự phải làm trước khi làm điều đó. Theo kinh nghiệm của tôi, cấu trúc này thường được sử dụng như là kết quả của sự lười biếng hơn bất cứ điều gì khác.