Hướng dẫn can you import all python? - bạn có thể nhập tất cả python?

Bài đăng này thảo luận về Python từ from import *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 = 141
def 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):
pass
class _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 = 141
def 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):
pass
class _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 = 42
_private_variable = 141
def 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):
pass
class _WeirdClass(object):
pass
4 và
# something.py__all__ = ['_private_variable', 'PublicClass']# The rest is the same as beforepublic_variable = 42
_private_variable = 141
def 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):
pass
class _WeirdClass(object):
pass
5:

>>> 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

  1. Tác giả đã xóa một chuỗi 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
    4. Nếu mã của bạn đang sử dụng tên đó, mã của bạn sẽ tăng
    >>> 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
    6 và nó sẽ khó tìm hiểu lý do tại sao.
  2. Tác giả đã thêm [nhiều] chuỗi mới vào
    >>> 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. 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.

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.

Bạn có thể nhập tất cả trong Python không?

Bạn có thể nhập tất cả mã từ một mô -đun bằng cách chỉ định từ khóa nhập theo sau là mô -đun bạn muốn nhập.Nhập báo cáo xuất hiện ở đầu tệp Python, bên dưới bất kỳ bình luận nào có thể tồn tại.Điều này là do nhập các mô -đun hoặc gói ở đầu tệp làm cho cấu trúc của mã của bạn rõ ràng hơn.. import statements appear at the top of a Python file, beneath any comments that may exist. This is because importing modules or packages at the top of a file makes the structure of your code clearer.

Làm thế nào nhập tất cả các gói Python?

Nếu bạn muốn xem danh sách các thư viện, hãy sử dụng DIR (PyForest).Để thêm các câu lệnh nhập của riêng bạn vào danh sách thư viện PyForest, hãy nhập câu lệnh nhập rõ ràng của bạn vào một tệp có trong thư mục nhà của bạn ~/.pyforest/user_imports.py.type in your explicit import statement to a file present in your home directory ~/. pyforest/user_imports.py .

Tôi có cần nhập trong mỗi tệp Python không?

Python không thực sự nhập một mô -đun mà nó đã nhập (trừ khi bạn buộc nó phải làm như vậy với chức năng tải lại), vì vậy bạn có thể đặt một câu lệnh nhập khẩu một số. (unless you force it to do so with the reload function), so you can safely put a import some_module statement into every module of your program that needs access to the names defined in some_module .

Có bao nhiêu nhập khẩu trong Python?

Có hai loại nhập khẩu tương đối: ngầm và rõ ràng.two types of relative imports: implicit and explicit.