__ mô-đun __ trong python là gì?

Bản tóm tắt. trong hướng dẫn này, bạn sẽ tìm hiểu về biến

import billing

Code language: Python (python)
1 trong Python và cách sử dụng nó hiệu quả trong các mô-đun

Python import billingCode language: Python (python)1 là gì?

Nếu bạn đã xem qua mã Python, có thể bạn đã thấy biến

import billing

Code language: Python (python)
1 như sau

if __name__ == '__main__': main()

Code language: Python (python)

Và bạn có thể thắc mắc biến

import billing

Code language: Python (python)
1 là gì

Vì biến

import billing

Code language: Python (python)
1 có hai dấu gạch dưới ở cả hai bên nên nó được gọi là tên dunder. Dunder là viết tắt của dấu gạch dưới kép

import billing

Code language: Python (python)
1 là một biến đặc biệt trong Python. Nó đặc biệt vì Python gán một giá trị khác cho nó tùy thuộc vào cách tập lệnh chứa nó thực thi

Khi bạn nhập một mô-đun, Python sẽ thực thi tệp được liên kết với mô-đun

Thông thường, bạn muốn viết một tập lệnh có thể được thực thi trực tiếp hoặc nhập dưới dạng mô-đun. Biến

import billing

Code language: Python (python)
1 cho phép bạn làm điều đó

Khi bạn chạy tập lệnh trực tiếp, Python sẽ đặt biến

import billing

Code language: Python (python)
1 thành

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
7

Tuy nhiên, nếu bạn nhập tệp dưới dạng mô-đun, Python sẽ đặt tên mô-đun thành biến

import billing

Code language: Python (python)
1

Ví dụ về biến import billingCode language: Python (python)1 của Python

Đầu tiên, tạo một mô-đun mới có tên là

import billing

Code language: Python (python)
0 có hai chức năng.

import billing

Code language: Python (python)
1 và

import billing

Code language: Python (python)
2. Ngoài ra, thêm câu lệnh in biến

import billing

Code language: Python (python)
1 ra màn hình

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)

Thứ hai, tạo một tệp mới có tên là

import billing

Code language: Python (python)
4 và nhập mô-đun

import billing

Code language: Python (python)
0

import billing

Code language: Python (python)

Khi bạn thực hiện

import billing

Code language: Python (python)
4

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
0

… biến

import billing

Code language: Python (python)
1 hiển thị giá trị sau

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
1

Điều đó có nghĩa là Python sẽ thực thi tệp

import billing

Code language: Python (python)
8 khi bạn nhập mô-đun thanh toán vào tệp

import billing

Code language: Python (python)
4

Biến

import billing

Code language: Python (python)
1 trong

import billing

Code language: Python (python)
4 được đặt thành tên mô-đun là

import billing

Code language: Python (python)
0

Nếu bạn thực thi trực tiếp

import billing

Code language: Python (python)
8 dưới dạng tập lệnh

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
7

… bạn sẽ thấy đầu ra sau

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
8

Trong trường hợp này, giá trị của biến

import billing

Code language: Python (python)
1 là

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
7 bên trong biến

import billing

Code language: Python (python)
8

__ mô-đun __ trong python là gì?
__ mô-đun __ trong python là gì?

Do đó, biến

import billing

Code language: Python (python)
1 cho phép bạn kiểm tra xem tệp được thực thi trực tiếp hay nhập dưới dạng mô-đun

Ví dụ: để thực thi hàm

import billing

Code language: Python (python)
2 khi

import billing

Code language: Python (python)
8 thực thi trực tiếp dưới dạng tập lệnh, bạn có thể thêm câu lệnh sau vào mô-đun

import billing

Code language: Python (python)
8

import billing

Code language: Python (python)
4

Thứ ba, thực thi

import billing

Code language: Python (python)
8 dưới dạng tập lệnh, bạn sẽ thấy đầu ra sau

import billing

Code language: Python (python)
6

Tuy nhiên, khi bạn thực thi

import billing

Code language: Python (python)
4, bạn sẽ không thấy khối

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
13 được thực thi vì biến

import billing

Code language: Python (python)
1 không được đặt thành

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
7 mà là

def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = [{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] # print billing header print(f'Name\tPrice\tTax') # print the billing item for product in products: tax = calculate_tax(product['price'], tax_rate) print(f"{product['name']}\t{product['price']}\t{tax}") print(__name__)

Code language: Python (python)
16

Ý nghĩa của if __ name __ == '__ Main__ là gì?

Tóm lại. Nó cho phép bạn thực thi mã khi tệp chạy dưới dạng tập lệnh , nhưng không phải khi nó được nhập dưới dạng mô-đun. Đối với hầu hết các mục đích thực tế, bạn có thể coi khối điều kiện mà bạn mở bằng if __name__ == "__main__" như một cách để lưu trữ mã chỉ chạy khi tệp của bạn được thực thi dưới dạng tập lệnh.

Tên __ __ trong Python là gì?

__name__ là một biến tích hợp dùng để đánh giá tên của mô-đun hiện tại . Do đó, nó có thể được sử dụng để kiểm tra xem tập lệnh hiện tại đang được chạy riêng hay được nhập ở nơi khác bằng cách kết hợp tập lệnh đó với câu lệnh if, như minh họa bên dưới.

__ gói __ là gì?

Tất cả những gì tôi muốn biết là __package__ chính xác nghĩa là gì . Có ba loại giá trị có thể có cho __package__ Tên gói (một chuỗi) Một chuỗi trống. the mechanism that enables explicit relative imports. There are three possible categories of values for __package__ A package name (a string) An empty string.

__ lớp __ trong Python là gì?

__class__ là một thuộc tính trên đối tượng đề cập đến lớp mà đối tượng được tạo ra từ đó . một. __lớp__ # Đầu ra.