Việc sử dụng câu lệnh if __name__ == “__main__” khá thường xuyên trong các tệp Python. Tuy nhiên, đôi khi nó có vẻ khó hiểu. Mục đích của bài viết này là khám phá hành vi của tuyên bố và thảo luận thêm về nơi sử dụng nó. Vậy hãy bắt đầu
Tóm lược. trong hướng dẫn này, bạn sẽ tìm hiểu về biến
1 trong Python và cách sử dụng nó hiệu quả trong các mô-đunCode language: Python [python]
import billing
Python import billing
Code language: Python [python]
1 là gì?
import billing
Nếu bạn đã xem qua mã Python, có thể bạn đã thấy biến
1 như sauCode language: Python [python]
import billing
Code language: Python [python]
if __name__ == '__main__': main[]
Và bạn có thể thắc mắc biến
1 là gìCode language: Python [python]
import billing
Vì biến
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épCode language: Python [python]
import billing
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 thiCode language: Python [python]
import billing
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
1 cho phép bạn làm điều đóCode language: Python [python]
import billing
Khi bạn chạy tập lệnh trực tiếp, Python sẽ đặt biến
1 thànhCode language: Python [python]
import billing
7
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]
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
1Code language: Python [python]
import billing
Ví dụ về biến import billing
Code language: Python [python]
1 của Python
import billing
Đầu tiên, tạo một mô-đun mới có tên là
0 có hai chức năng.Code language: Python [python]
import billing
1 vàCode language: Python [python]
import billing
2. Ngoài ra, thêm câu lệnh in biếnCode language: Python [python]
import billing
1 ra màn hìnhCode language: Python [python]
import billing
Code language: Python [python]
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__]
Thứ hai, tạo một tệp mới có tên là
4 và nhập mô-đunCode language: Python [python]
import billing
0Code language: Python [python]
import billing
Code language: Python [python]
import billing
Khi bạn thực hiện
4Code language: Python [python]
import billing
0Code language: Python [python]
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__]
… biến
1 hiển thị giá trị sau
import billing
Code language: Python [python]
1Code language: Python [python]
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__]
Điều đó có nghĩa là Python sẽ thực thi tệp
8 khi bạn nhập mô-đun thanh toán vào tệpCode language: Python [python]
import billing
4Code language: Python [python]
import billing
Biến
1 trongCode language: Python [python]
import billing
4 được đặt thành tên mô-đun làCode language: Python [python]
import billing
0Code language: Python [python]
import billing
Nếu bạn thực thi trực tiếp
8 dưới dạng tập lệnhCode language: Python [python]
import billing
7Code language: Python [python]
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__]
… bạn sẽ thấy đầu ra sau
8Code language: Python [python]
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__]
Trong trường hợp này, giá trị của biến
1 làCode language: Python [python]
import billing
7 bên trong biếnCode language: Python [python]
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__]
8Code language: Python [python]
import billing
Do đó, biến
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ô-đunCode language: Python [python]
import billing
Ví dụ: để thực thi hàm
2 khiCode language: Python [python]
import billing
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ô-đunCode language: Python [python]
import billing
8Code language: Python [python]
import billing
4Code language: Python [python]
import billing
Thứ ba, thực thi
8 dưới dạng tập lệnh, bạn sẽ thấy đầu ra sauCode language: Python [python]
import billing
6Code language: Python [python]
import billing
Tuy nhiên, khi bạn thực thi
4, bạn sẽ không thấy khốiCode language: Python [python]
import billing
13 được thực thi vì biếnCode language: Python [python]
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__]
1 không được đặt thànhCode language: Python [python]
import billing
7 mà làCode language: Python [python]
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__]
16Code language: Python [python]
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__]