__ tất cả __ làm gì trong python?

Hệ thống nhập của Python hoạt động rất trực quan theo thiết kế. Nhập toàn bộ mô-đun hoặc biến đơn lẻ có thể được thực hiện bằng cú pháp một dòng. Đối với các trường hợp toàn bộ gói đang được nhập, việc triển khai tùy chỉnh biến __all__ có thể giúp hạn chế ý nghĩa thực sự của “tất cả”

Mục lục

Hệ thống nhập của Python, cùng nhịp với nhiều hệ thống nhập của ngôn ngữ lập trình khác, cho phép sử dụng toán tử ký tự đại diện *. Điều này hướng dẫn Python nhập “tất cả” nội dung của mô-đun. Theo mặc định, điều này bao gồm

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
7,
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
8,
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
9 và mọi thứ khác không có tên có tiền tố gạch dưới

Triển khai __all__ tùy chỉnh

Biến

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
0 trong Python rất trực quan để sử dụng nhưng có thể khó diễn đạt bằng lời. Hãy xem xét đoạn mã sau

examples/custom.py

# Import a library
import random

# Define constants
__COLORS__ = ["red", "blue", "yellow", "green"]
__SHAPES__ = ["round", "square", "cylindrical"]

# Define a custom class
class Candy:
    """
    An object model for a piece of candy expressing color and shape
    """
    def __init__[self, color: str, shape: str]:
        self.color = color
        self.shape = shape

    def __str__[self]:
        return f"A tasty piece of {self.shape} {self.color} candy"

# Define a custom function
def get_random_candy[]:
    """
    Creates a random Candy object
    Returns:
        Candy object
    """
    return Candy[random.choice[__COLORS__], random.choice[__SHAPES__]]

# Create a random piece of candy
random_candy = get_random_candy[]

Lựa chọn mã giả tạo này xác định nhiều loại đối tượng bao gồm

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
9,
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
8,
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
7 và thậm chí nhập một gói khác. Bây giờ, hãy xem xét việc sử dụng tệp này trong một tệp Python khác

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]

Tệp này truy cập mọi thứ chúng tôi đã tạo trong các ví dụ của mình. tập tin py ban đầu. Khi chúng tôi chạy mã này, chúng tôi gặp sự cố khi tạo

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
1, truy cập vào các biến có tiền tố gạch dưới kép

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
2

Điều này xảy ra vì mọi thứ trong Python đều có tiền tố gạch dưới [bao gồm cả dấu gạch dưới kép] theo mặc định. Để đảm bảo các đối tượng này được nhập trong các câu lệnh nhập ký tự đại diện, các mô-đun phải xác định chúng một cách rõ ràng. Để đạt được điều này, chúng tôi sẽ thêm đoạn mã sau vào tệp

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
3

__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']

Bây giờ, khi chúng tôi chạy mã từ tệp

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
4 của mình, không có lỗi. Điều này là do các hằng số
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
5 và
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
6 đã được thêm rõ ràng vào xuất mô-đun thông qua biến
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
0

Sử dụng __all__ để xóa nhập/xuất rõ ràng

Biến

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
0 của Python rất hữu ích để bao gồm các biến có tiền tố gạch dưới mà nếu không sẽ không được nhập. Tuy nhiên, điều này mang lại một mức độ trách nhiệm bổ sung nhất định cho các nhà phát triển. mọi thứ dành cho xuất khẩu phải được thêm rõ ràng vào biến
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
0. Điều đó có nghĩa là loại bỏ một
__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
0,
__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
1,
__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
2 hoặc dạng đối tượng khác lẽ ra đã được xuất theo mặc định—hiện dẫn đến đối tượng đó không được xuất. Hãy xem xét đoạn mã sau

examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
2

Điều này hiện dẫn đến một ngoại lệ

__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
3 nơi chúng tôi đang gọi hàm
__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
4. Thông thường, trong một câu lệnh
__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
5, chức năng như chức năng này sẽ được đưa vào theo mặc định. Tuy nhiên, vì chúng tôi đã ghi đè biến
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
0 mặc định—mọi thứ không được bao gồm sẽ không được nhập

Suy nghĩ cuối cùng

Tôi thuộc trường phái sử dụng

__all__ = ['__COLORS__', '__SHAPES__', 'Candy', 'get_random_candy', 'random_candy', 'random']
7 hiếm khi là một ý kiến ​​hay. Như vậy, tôi thấy mình có rất ít nhu cầu sử dụng phương pháp này để tùy chỉnh biến
examples/custom_two.py

# Import everything from custom file
from custom import *

# Access variable
candy_1 = random_candy

# Create new object using function
candy_2 = get_random_candy[]

# Create new object using Class
candy_3 = Candy['cyan', 'triangular']

# Create new object using Class using protected constants
candy_4 = Candy[__COLORS__[0], __SHAPES__[1]]

# Access imported library
random_number = random.choice[[0, 1, 2, 3, 4]]
0. Tuy nhiên, biết nó là gì và cách sử dụng nó là hữu ích—nếu không vì lý do nào khác ngoài việc có thể hiểu các dự án Python khiến việc sử dụng phương pháp này trở nên nặng nề hơn

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

__ tệp __ có nghĩa là gì trong Python?

__file__ là tên đường dẫn của tệp mà mô-đun được tải từ đó , nếu nó được tải từ một tệp.

Các biến đặc biệt 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.

Biến dunder trong Python là gì?

Biến gạch dưới kép trong Python thường được gọi là dấu gạch dưới. Biến dunder là một biến mà Python đã định nghĩa để nó có thể sử dụng nó theo “Cách đặc biệt”. Cách đặc biệt này phụ thuộc vào biến đang được sử dụng.

Chủ Đề