__ biến __ có nghĩa là gì trong python?

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 billing

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

Code 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

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 là gì trong Python?

Việc sử dụng dấu gạch dưới kép [ __ ] trước tên [cụ thể là tên phương thức] không phải là quy ước; . Python sắp xếp các tên này và nó được sử dụng để tránh xung đột tên với các tên được xác định bởi các lớp con. it has a specific meaning to the interpreter. Python mangles these names and it is used to avoid name clashes with names defined by subclasses.

Biến _ có nghĩa là gì trong Python?

Dấu gạch dưới một bài đăng được sử dụng để đặt tên biến của bạn là Từ khóa Python và để tránh xung đột bằng cách thêm dấu gạch dưới ở cuối tên biến của bạn .

__ tên __ biến trong Python là gì?

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 __ dùng để làm gì?

__name__ là 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.

Chủ Đề