Hướng dẫn what does __ main __ mean in python? - __ main __ có nghĩa là gì trong python?

Hướng dẫn what does __ main __ mean in python? - __ main __ có nghĩa là gì trong python?

Khi một trình thông dịch Python đọc một tệp Python, trước tiên nó sẽ đặt một vài biến đặc biệt. Sau đó, nó thực thi mã từ tệp.

Một trong những biến đó được gọi là

File one __name__ is set to: __main__
9.

Nếu bạn theo dõi bài viết này từng bước và đọc đoạn mã của nó, bạn sẽ tìm hiểu cách sử dụng

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
0 và tại sao nó lại quan trọng như vậy.

Các mô -đun Python giải thích

Các tệp Python được gọi là các mô -đun và chúng được xác định bởi phần mở rộng tệp

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
1. Một mô -đun có thể xác định các chức năng, lớp và biến.

Vì vậy, khi trình thông dịch chạy một mô -đun, biến

File one __name__ is set to: __main__
9 sẽ được đặt là & nbsp; ____ 23 nếu mô -đun đang được chạy là chương trình chính.

Nhưng nếu mã đang nhập mô -đun từ một mô -đun khác, thì biến

File one __name__ is set to: __main__
9 & NBSP; sẽ được đặt thành tên mô -đun đó.

Chúng ta hãy xem một ví dụ. Tạo một mô -đun Python có tên

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
5 và dán mã cấp cao nhất bên trong:

# Python file one module

print("File one __name__ is set to: {}" .format(__name__))
File_One.py

Bằng cách chạy tập tin này, bạn sẽ thấy chính xác những gì chúng ta đang nói. Biến

File one __name__ is set to: __main__
9 cho mô -đun này được đặt thành
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3:

File one __name__ is set to: __main__

Bây giờ thêm một tệp khác có tên

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
8 và dán mã này bên trong:

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
file_two.py

Ngoài ra, sửa đổi mã trong

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
5 như thế này vì vậy chúng tôi nhập mô -đun
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0:

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
file_one.py

Chạy mã

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1 của chúng tôi một lần nữa sẽ cho thấy biến
File one __name__ is set to: __main__
9 trong
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1 không thay đổi và vẫn còn được đặt thành
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3. Nhưng bây giờ biến
File one __name__ is set to: __main__
9 trong
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 được đặt làm tên mô -đun của nó, do đó
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0.

Kết quả sẽ trông như thế này:

File two __name__ is set to: file_two
File one __name__ is set to: __main__

Nhưng chạy

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 trực tiếp và bạn sẽ thấy rằng tên của nó được đặt thành
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3:

File two __name__ is set to: __main__

Biến

File one __name__ is set to: __main__
9 cho tệp/mô -đun được chạy sẽ luôn luôn là
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3. Nhưng biến
File one __name__ is set to: __main__
9 cho tất cả các mô -đun khác đang được nhập sẽ được đặt thành tên của mô -đun của họ.

Các quy ước đặt tên tệp Python

Cách sử dụng thông thường

File one __name__ is set to: __main__
9 và
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3 trông như thế này:

if __name__ == "__main__":
   Do something here

Hãy xem cách thức hoạt động trong cuộc sống thực và cách sử dụng các biến này thực sự.

Sửa đổi

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1 và
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 để trông như thế này:

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1:

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))

if __name__ == "__main__":
   print("File one executed when ran directly")
else:
   print("File one executed when imported")
file_one.py

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0:

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))

if __name__ == "__main__":
   print("File two executed when ran directly")
else:
   print("File two executed when imported")
file_two.py

Một lần nữa, khi chạy

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1, bạn sẽ thấy chương trình được nhận ra trong hai mô -đun này là
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3 và thực hiện mã theo các câu lệnh
File two __name__ is set to: __main__

1 đầu tiên của chúng tôi.

Kết quả sẽ trông như thế này:

File two __name__ is set to: file_two
File two executed when imported
File one __name__ is set to: __main__
File one executed when ran directly

Nhưng chạy

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 trực tiếp và bạn sẽ thấy rằng tên của nó được đặt thành
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3:

File one __name__ is set to: __main__
0

Biến

File one __name__ is set to: __main__
9 cho tệp/mô -đun được chạy sẽ luôn luôn là
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3. Nhưng biến
File one __name__ is set to: __main__
9 cho tất cả các mô -đun khác đang được nhập sẽ được đặt thành tên của mô -đun của họ.

Các quy ước đặt tên tệp Python

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1:

Cách sử dụng thông thường
File one __name__ is set to: __main__
9 và
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3 trông như thế này:

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0:

File one __name__ is set to: __main__
2

Hãy xem cách thức hoạt động trong cuộc sống thực và cách sử dụng các biến này thực sự.

Sửa đổi

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1 và
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 để trông như thế này:

File one __name__ is set to: __main__
3

Một lần nữa, khi chạy

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1, bạn sẽ thấy chương trình được nhận ra trong hai mô -đun này là
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3 và thực hiện mã theo các câu lệnh
File two __name__ is set to: __main__

1 đầu tiên của chúng tôi.

File one __name__ is set to: __main__
4

Bây giờ chạy

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 và bạn sẽ thấy biến
File one __name__ is set to: __main__
9 được đặt thành
# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
3:

File one __name__ is set to: __main__
5

Khi các mô -đun như thế này đang được nhập và chạy, các chức năng của chúng sẽ được nhập và mã cấp cao nhất được thực thi.

File one __name__ is set to: __main__
6

Để xem quá trình này hoạt động, hãy sửa đổi các tệp của bạn để trông như thế này:

File one __name__ is set to: __main__
7file_two.py

File one __name__ is set to: __main__
1File_One.py

File one __name__ is set to: __main__
8file_one.py

Bây giờ các chức năng được tải nhưng không chạy.

Để chạy một trong những chức năng này sửa đổi phần

# Python module to import

print("File two __name__ is set to: {}" .format(__name__))
0 của
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1 để trông như thế này:

Khi chạy

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1, bạn sẽ thấy như thế này:

Ngoài ra, bạn có thể chạy các chức năng từ các tệp đã nhập. Để làm điều đó, hãy sửa đổi phần

if __name__ == "__main__":
   Do something here

0 của
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
1 để trông như thế này:

Và bạn có thể mong đợi một kết quả như thế này:



Bây giờ giả sử mô -đun

# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 thực sự rất lớn với rất nhiều chức năng (hai trong trường hợp của chúng tôi) và bạn không muốn nhập tất cả chúng. Sửa đổi
# Python module to execute
import file_two

print("File one __name__ is set to: {}" .format(__name__))
0 để trông như thế này:

Nếu __ name__ chính 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ể nghĩ về khối có điều kiện mà bạn mở với nếu __name__ == "__main__" như một cách để lưu trữ mã chỉ nên chạy khi tệp của bạn được thực thi dưới dạng tập lệnh.It Allows You to Execute Code When the File Runs as a Script, but Not When It's Imported as a Module. For most practical purposes, you can think of the conditional block that you open with if __name__ == "__main__" as a way to store code that should only run when your file is executed as a script.

Làm thế nào để __ tên __ hoạt động trong Python?

Biến __name__ (hai dấu gạch dưới 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 một tập lệnh khác.It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.

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

Chức năng chính giống như điểm vào của một chương trình.Tuy nhiên, trình thông dịch Python chạy mã ngay từ dòng đầu tiên.Việc thực hiện mã bắt đầu từ dòng đầu tiên và đi từng dòng.Không quan trọng nơi có chức năng chính có mặt hoặc có mặt hay không.like the entry point of a program. However, Python interpreter runs the code right from the first line. The execution of the code starts from the starting line and goes line by line. It does not matter where the main function is present or it is present or not.

__ init __ py là gì?

Các tệp __init__.py được yêu cầu để làm cho các thư mục xử lý Python chứa tệp dưới dạng các gói.Điều này ngăn các thư mục có tên chung, chẳng hạn như chuỗi, vô tình ẩn các mô -đun hợp lệ xảy ra sau đó trên đường dẫn tìm kiếm mô -đun.required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.