Nếu __ tên __ trong python thì sao?

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

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

Nếu __ tên __ trong python thì sao?
Nếu __ tên __ trong python thì sao?

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

__ tên __ có nghĩa là gì Python?

Biến __name__ (hai dấu gạch dưới trước và sau) là một biến Python đặc biệt . Nó nhận được giá trị của nó tùy thuộc vào cách chúng tôi thực thi tập lệnh chứa. Đôi khi bạn viết một tập lệnh với các chức năng cũng có thể hữu ích trong các tập lệnh khác. Trong Python, bạn có thể nhập tập lệnh đó dưới dạng mô-đun trong tập lệnh khác.

__ tên __ == '__ chính __' trong Python là gì?

Trong Python, tên đặc biệt __main__ được sử dụng cho hai cấu trúc quan trọng. tên môi trường cấp cao nhất của chương trình , có thể kiểm tra bằng biểu thức __name__ == '__main__'; . __chính__. py trong các gói Python.

Điểm của __ tên __ là gì?

__name__ == "__main__" chỉ chạy các khối mã khi tập lệnh Python của chúng tôi đang được thực thi trực tiếp từ người dùng . Điều này rất hữu ích vì nó cho phép mã của chúng ta có hành vi khác khi nó được thực thi dưới dạng chương trình thay vì được nhập dưới dạng mô-đun.

Việc sử dụng __ chính __ trong Python là gì?

Các tệp Python có thể hoạt động như các mô-đun có thể tái sử dụng hoặc dưới dạng các chương trình độc lập. nếu __name__ == “chính”. chỉ được sử dụng để thực thi một số mã nếu tệp được chạy trực tiếp và không được nhập