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à
9.File one __name__ is set to: __main__
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.pyBằ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.pyNgoà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.pyChạ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.pyMộ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__
0Biế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__
2Hã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__
3Mộ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__
4Bâ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__
5Khi 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.pyFile one __name__ is set to: __main__
8file_one.pyBâ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: