Nhập Python nếu không được nhập

Bài đăng này thảo luận về

>>> 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 và
>>> 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 của Python, cách chúng hoạt động 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 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 có nghĩa là “Tôi muốn truy cập vào tất cả các tên trong
>>> 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 mà tôi muốn có quyền truy cập”. Vì vậy, giả sử chúng ta có
>>> 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 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
>>> 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 ở 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
>>> 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

>>> 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ây giờ, chúng tôi mong muốn

>>> 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
>>> 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
34 và
>>> 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
35

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

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

Tuy nhiên, điểm khác biệt là, khi bạn bỏ 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
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
70, thì 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
0 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
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
72 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 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 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 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â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
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
74 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
    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
    76 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 việc nhập

>>> 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
78 từ các mô-đun và/hoặc gói có thể hữu ích hoặc cần thiết. 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

Làm cách nào để kiểm tra xem một mô-đun Python có tồn tại mà không cần nhập nó không?

Trăn 3 >= 3. 6. ModuleNotFoundError . sẽ in một thông báo giống như Không có mô-đun có tên 'eggs' nếu không tìm thấy mô-đun trứng ; . sub' nếu chỉ không tìm thấy mô-đun phụ, nhưng có thể tìm thấy gói egg.

Tại sao nhập * không được khuyến khích?

Sử dụng nhập * trong các chương trình python được coi là một thói quen xấu vì theo cách này, bạn đang làm ô nhiễm không gian tên của mình , câu lệnh nhập * sẽ nhập tất cả .

Bạn có thể nhập câu lệnh if Python không?

Câu lệnh nhập chỉ là sự liên kết lại của một đoạn mã bên ngoài với tên cục bộ. Việc sử dụng luồng điều khiển if để kiểm soát quá trình nhập không khác gì việc gán một biến trong câu lệnh if về vấn đề đó . Bạn cần đảm bảo rằng bạn sẽ không sử dụng tên mà không được xác định theo bất kỳ cách nào.

Tôi có thể nhập __ init __ không?

Có hai loại gói trong python, gói thông thường và gói không gian tên. Cái trước yêu cầu __init__. py trong khi cái sau thì không. Bất kỳ thư mục nào có tệp init python đều được python đánh dấu là gói và có thể được nhập .

Chủ Đề