Nhập từ có nghĩa là gì trong Python?

Bài đăng này thảo luận về from import *from import * của Python, cách chúng hoạt động và lý do 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 muốn có quyền truy cập”. Vì vậy, giả sử chúng ta có something.py sau

# 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 phần 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 ngoại trừ các tên bắt đầu bằ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
3;

Ermm, điều đó không quá tệ. Đó có phải là `__all__` không?

Những gì không được đề cập ở trên là những 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à.
>>> 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
5 là danh sách các chuỗi xác định ký hiệu nào trong một mô-đun [hoặc một gói như chúng ta sẽ thấy sau] sẽ được xuất khi
>>> 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
6 đượ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 xác định trong something.py ở trên], 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ừ những 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ì theo quy ước, dấu gạch dưới được sử dụng để nói ký hiệu là , nên tất cả đều có ý nghĩa. Hãy xem điều gì sẽ 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 riêng 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 mong đợ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
0 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 các gói thì sao?

Khi ,

>>> 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 gần giống như đối với các mô-đun, ngoại trừ nó xử lý các mô-đun trong gói [ngược lại 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, điểm khác biệt là, khi bạn lược bỏ phần khai bá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 trong gói hà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
0, thì phần khai báo from import * sẽ không nhập khẩu gì cả [không hoàn toàn đúng; vì 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 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 The Zen of Python [cũng đọc cho con bạn nghe mỗi tối trước khi chúng đi ngủ]

Rõ ràng là tốt hơn ngầm

from import * không rõ ràng. Nó không cho chúng tôi biết bất cứ điều gì về những gì chúng tôi đang nhập hoặc những tên chúng tôi đang đưa vào không gian tên của mình. Sẽ tốt hơn nhiều nếu chỉ định rõ ràng và nhập mọi thứ chúng ta cần; . được sử dụng trong mã đến từ đâu, điều này dẫn chúng ta đến

số lượng khả năng đọc

Ngay cả khi có nhiều tên được yêu cầu, việc nhập từng cái một vẫn rõ ràng hơn nhiều. 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 mình 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 chóng [nói theo nghĩa bóng] có thể cho bạn biết tên đến từ đâu

Ngoài ra, bạn luôn gặp rủi ro nếu/khi tác giả mô-đun/gói quyết định thay đổi nội dung của danh sách [thêm/xóa nội dung vào/khỏi nó]. Sau đó, một trong hai

  1. Tác giả đã xóa một chuỗi khỏ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. 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à sẽ khó tìm ra 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ỉ đang làm đầy không gian tên của mình bằng những thứ bạn không quan tâm. Họ thậm chí có thể che giấu 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, tốt nhất bạn nên chắc chắn rằng mình thực sự phải làm vậy trước khi thực hiện. Theo kinh nghiệm của tôi, cấu trúc này thường được sử dụng do lười biếng hơn bất kỳ thứ gì khác

Sự khác biệt giữa nhập và nhập từ trong Python là gì?

Sự khác biệt giữa nhập và từ nhập trong Python là. import nhập toàn bộ thư viện mã. từ nhập nhập một thành viên cụ thể hoặc các thành viên của thư viện .

Làm cách nào để Python biết nhập từ đâu?

Trình thông dịch python cố gắng tìm thư mục chứa mô-đun mà chúng tôi đang cố gắng nhập vào sys. đường dẫn . Đó là danh sách các thư mục mà Python sẽ tìm kiếm sau khi tìm kiếm xong các mô-đun được lưu trong bộ nhớ cache và các mô-đun thư viện chuẩn của Python.

Nhập từ DOT có nghĩa là gì trong Python?

Trong câu lệnh nhập đầu tiên, dấu chấm đơn có nghĩa là bạn đang nhập lớp 1 từ gói hiện tại .

Việc sử dụng từ trong Python là gì?

Từ khóa from được sử dụng để chỉ nhập một phần được chỉ định từ một mô-đun .

Chủ Đề