Vì không có hàm main() trong Python, khi lệnh chạy chương trình python được đưa cho trình thông dịch, mã ở mức thụt lề cấp 0 sẽ được thực thi. Tuy nhiên, trước khi làm điều đó, nó sẽ xác định một vài biến đặc biệt. __name__ là một trong những biến đặc biệt như vậy. Nếu tệp nguồn được thực thi dưới dạng chương trình chính, trình thông dịch đặt biến __name__ có giá trị “__main__”. Nếu tệp này đang được import từ một mô-đun khác, __name__ sẽ được đặt thành tên của mô-đun. __name__ là một biến được tích hợp sẵn để đánh giá tên của mô-đun hiện tại. Vì vậy, 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 import vào một nơi khác bằng cách kết hợp nó với câu lệnh if, như được hiển thị bên dưới. Hãy xem xét hai tệp riêng biệt File1 và File2. # File1.py print ("File1 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File1 is being run directly")
else:
print ("File1 is being imported")
File2.py import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported")
output Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly
Như đã thấy ở trên, khi File1.py được chạy trực tiếp, trình thông dịch đặt biến __name__ là __main__ và khi nó được chạy qua File2.py bằng cách import, biến __name__ được đặt làm tên của tập lệnh python, tức là File1. Do đó, có thể nói rằng nếu __name__ == “__main__” là một phần của chương trình chạy khi tập lệnh được chạy từ dòng lệnh bằng cách sử dụng lệnh như python File1.py. Cài ứng dụng cafedev để dễ dàng cập nhật tin và học lập trình mọi lúc mọi nơi tại đây.mọi lúc mọi nơi tại đây. Nguồn và Tài liệu tiếng anh tham khảo: Tài liệu từ cafedev: - Full series tự học Python từ cơ bản tới nâng cao tại đây nha.
- Tự học ML bằng Python từ cơ bản tới nâng cao.
- Các nguồn kiến thức MIỄN PHÍ VÔ GIÁ từ cafedev tại đâycafedev tại đây
Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa: - Group Facebook
- Fanpage
- Youtube
- Instagram
- Twitter
- Linkedin
- Pinterest
- Trang chủ
Chào thân ái và quyết thắng! Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!Mục đích của câu lệnh if __name__ == "__main__" trong Python là gì? Mình thường thấy các chương trình Python thường hay kèm theo câu if __name__ == '__main__' ở cuối sau khi viết các hàm, ví dụ, def print_hello():
print "Hello"
if __name__ == '__main__':
print_hello()
Mình thử xóa đi câu lệnh này đi, chương trình vẫn chạy bình thường, vậy câu lệnh này dùng để làm gì ạ? Trong Python, khi bạn viết một chương trình hoặc một module nhỏ trong một chương trình lớn thì chắc chắn mã nguồn sẽ được lưu dưới dạng là một file có đuôi mở rộng là .py , ví dụ example.py chẳng hạn, làm sao để bạn sử dụng file mã nguồn này? Chắc chắn bạn đã biết sẽ có 2 cách để sử dụng đúng không nào: Cách 1 : Thực thi mã nguồn Python trực tiếp bằng câu lệnh console của hệ điều hành (Command Line)
Ví dụ import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 0import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 1: import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 2 mã nguồn Python vào trong một file mã nguồn Python khác.Ví dụ mình viết một số hàm trong file Python import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 3, mình tạo một file Python khác tên là import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 4, trong file import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 4 mình muốn sử dụng hàm của file import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 3, mình chỉ việc sử dụng từ khóa import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 2 là xong (cái này thì bạn nào lập trình Python chắc chắn là biết rồi).# bar.py
import foo
Thì mục đích của câu lệnh import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 8 dùng để xác định rằng file Python đang được thực thi trực tiếp theo cách 1 hay là đang được import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 2 trong một chương trình Python khác:Nếu file Python được thực thi trực tiếp bằng Command Line thì biến Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 0 sẽ bằng Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 1 và một số câu lệnh bên trong hàm import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 8 sẽ được thực hiện vì Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 3 sẽ trả về Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 4.Nếu file Python được import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 2 thành module của chương trình Python khác thì biến Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 0 sẽ bằng tên của chương trình module đó, ví dụ nếu import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 3 được import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 2 trong import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 4 thì biến def print_hello():
print "Hello"
if __name__ == '__main__':
print_hello()
1 và lệnh import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 8 trả về def print_hello():
print "Hello"
if __name__ == '__main__':
print_hello()
3, tất nhiên một số câu lệnh bên trong hàm Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 3 sẽ không được thực hiện.
Python cho phép điều này để chi? Giả sử bạn muốn một số đoạn def print_hello():
print "Hello"
if __name__ == '__main__':
print_hello()
5 chỉ được thực thi khi bạn chạy trực tiếp bằng Command Line mà không được thực thi khi bị import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 2 thành module thì bạn sử dụng câu lệnh này, việc này rất hữu ích khi bạn viết xong một hàm, bạn không chắc là hàm đó có chạy đúng không, bạn sẽ viết thêm một số đoạn code khác ngay bên dưới nó để test thử hàm đó, khi ok rồi, bạn sử dụng lại file Python chứa hàm bạn đã viết ở một chương trình khác thì đoạn code mà lúc bạn viết để test thử bạn có muốn nó cũng thực thi trong khi sử dụng hay không? Tất nhiên là không:- Một là bạn xóa nó đi.
- Hai là bạn comment đoạn mã nguồn test thử đó lại.
- Ba là sử dụng câu lệnh
import File1
print ("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print ("File2 is being run directly")
else:
print ("File2 is being imported") 8 và đặt đoạn mã nguồn test thử vào bên trong Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly 3 là xong.
Python không muốn bạn phải phiền hà với những chi tiết lặt vặt khi lập trình, đó chỉ là cách giúp bạn lập trình dễ dàng hơn, tránh bị lỗi và xử lý chuyên nghiệp hơn. Chào mừng đến với cộng đồng Python :)). • đã đăng 4.2 năm trước bởi đã bổ sung 4.2 năm trước bởi Bạn chưa đăng nhập, vui lòng đăng nhập để thêm câu trả lời. Bạn đang thắc mắc? Ghi câu hỏi của bạn và đăng ở chế độ cộng đồng (?)
|