__name__ == __main__ con trăn

Vì không có hàm main[] trong Python, khi lệnh chạy chương trình python được đưa cho thông dịch trình, mã ở mức nhận biên 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 nguồn gốc được thực hiện dưới dạng chương trình chính, thông tin dịch trình đặt biến __name__ có giá trị “__main__”. Nếu tệp này đang được nhập 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 đang được chạy riêng hay được nhập vào một nơi khác bằng cách kết hợp nó với lệnh nếu như được hiển thị bên dưới

Vui lòng xem xét hai tệp riêng biệt File1 và File2

# Tệp1. py

print ["File1 __name__ = %s" %__name__] 
  
if __name__ == "__main__": 
    print ["File1 is being run directly"]
else: 
    print ["File1 is being imported"]

Tệp2. py

import File1 
  
print ["File2 __name__ = %s" %__name__] 
  
if __name__ == "__main__": 
    print ["File2 is being run directly"]
else: 
    print ["File2 is being imported"]

đầu ra

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, thông dịch trình đặt biến __name__ là __main__ và khi nó được chạy qua File2. py by way import, variable __name__ was set to name of the python command file, 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 đang 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 đặt ứng dụng cafedev để dễ dàng cập nhật tin tức và học lập trình mọi lúc mọi nơi tại đây

Nguồn và Tài liệu tiếng anh tham khảo

  • con trăn. tổ chức

Tài liệu từ cafedev

  • Trọn bộ tự học Python từ cơ bản tăng cao tại đây nha
  • Tự học ML bằng Python từ cơ bản để nâng cao
  • Các nguồn kiến ​​thức thức bổ trợ VÔ GIÁ từ cafedev tại đây

Nếu 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

Nhiều ngôn ngữ lập trình có một chức năng đặc biệt được thực thi tự động khi hệ thống điều hành bắt đầu chạy một chương trình. Hàm này thường được gọi là 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4và phải có kiểu trả về cụ thể và các đối số theo tiêu chuẩn ngôn ngữ. Mặt khác, trình thông dịch Python thực thi các lệnh bắt đầu tập tin từ tệp đầu và không có chức năng cụ thể nào mà Python tự động thực thi

Tuy nhiên, có một điểm bắt đầu xác định để thực hiện một chương trình rất hữu ích để hiểu cách thức hoạt động của một chương trình. Trình lập trình viên Python đã đưa ra một số quy ước để xác định điểm bắt đầu này

Đến cuối bài viết này, bạn sẽ hiểu

  • print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    5Biến đặc biệt là gì và cách Python định nghĩa nó
  • Tại sao bạn muốn sử dụng một 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4trong Python
  • Có những quy ước nào để xác định 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4trong Python
  • Các phương pháp hay nhất là gì để đưa mã vào 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4

Tải xuống miễn phí. Nhận một chương trình mẫu từ thủ thuật Python. Cuốn sách chỉ dành cho bạn các phương pháp hay nhất của Python với các ví dụ đơn giản mà bạn có thể áp dụng ngay lập tức để viết mã + Pythonic đẹp hơn

Một Python cơ bản chính []

Trong một số tập lệnh Python, bạn có thể tìm thấy định nghĩa hàm và điều kiện câu lệnh giống như ví dụ bên dưới

________số 8_______

Trong đoạn mã này, có một hàm được gọi là 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4in ra cụm từ 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
0khi trình thông dịch Python thực thi nó. Ngoài ra còn có một 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
1câu lệnh điều kiện [hoặc ] kiểm tra giá trị của 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5và so sánh nó với chuỗi 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
3. Khi 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
1câu lệnh đánh giá là 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
5, trình thông dịch Python sẽ thực thi 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4. Bạn có thể đọc thêm về câu lệnh điều kiện trong Câu lệnh điều kiện trong Python

Mẫu mã này khá phổ biến trong các tệp Python mà bạn muốn được thực hiện dưới dạng lệnh của tệp và được nhập trong một mô-đun khác. Để giúp hiểu cách mã hóa này sẽ thực thi, trước tiên bạn nên hiểu cách trình thông dịch Python đặt 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5tùy thuộc vào cách mã hóa đang được thực thi

Thi mode mode by Python

Có hai cách chính mà bạn có thể hướng dẫn trình thông dịch Python thực thi hoặc sử dụng mã

  1. Bạn có thể thực thi tệp Python dưới dạng tập lệnh này bằng cách sử dụng dòng lệnh
  2. You can nhập mã từ tệp Python vào tệp khác hoặc vào trình tương tác thông tin

Bạn có thể đọc thêm nhiều điều về các cách tiếp cận này trong Cách chạy tập lệnh Python của bạn. Bất kể bạn đang sử dụng cách chạy mã nào, Python xác định một biến đặc biệt được gọi là biến 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5chứa một chuỗi có giá trị phụ thuộc vào cách mã đang được sử dụng

Chúng tôi sẽ sử dụng tệp ví dụ này, được lưu dưới dạng 

$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
9, để khám phá cách hành động của mã thay đổi tùy thuộc vào ngữ cảnh

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]

Trong tệp này, có ba lệnh gọi đến 

>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0đã được xác định. Hai đầu tiên trong một số cụm từ giới thiệu. Đầu 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0tiên, thứ ba sẽ nằm trong cụm từ 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
2, và sau đó nó sẽ nằm trong phần biểu diễn của 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5 biến bằng cách sử dụng tích hợp sẵn của Python 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
4

Trong Python, 

>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
4hiển thị biểu diễn có thể có trong một đối tượng. Ví dụ này sử dụng 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
4 để nhấn mạnh rằng giá trị của 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5là một chuỗi. Bạn có thể đọc thêm 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
4trong

Bạn sẽ thấy tep từ , mô-đun và tập lệnh được sử dụng trong suốt bài viết này. Thực tế, không có nhiều sự khác biệt giữa chúng. Tuy nhiên, có những khác biệt nhỏ về ý nghĩa nhấn mạnh mục tiêu của đoạn mã

  1. File. Thông thường, tệp Python là bất kỳ tệp nào có chứa mã. Hầu hết các tệp Python đều có phần mở rộng 

    >>> import execution_methods
    This is my file to test Python's execution methods.
    The variable __name__ tells me which context this file is running in.
    The value of __name__ is: 'execution_methods'
    
    9

  2. Command command. Lệnh Python là một tệp mà bạn dự kiến ​​thực thi từ dòng lệnh để hoàn thành một nhiệm vụ

  3. mô-đun. Mô-đun Python là một tệp mà bạn dự kiến ​​nhập từ bên trong một mô-đun khác hoặc một tệp lệnh hoặc từ một trình thông dịch tương tác. Bạn có thể đọc thêm về các mô-đun trong Mô-đun và Gói Python - Giới thiệu

Sự khác biệt này cũng được thảo luận trong Cách chạy tập lệnh Python của bạn

Thực thi từ dòng lệnh

Trong cách tiếp cận này, bạn muốn thực thi lệnh Python của mình từ dòng lệnh

Khi bạn thực thi một lệnh tập tin, bạn sẽ không thể xác định mã tương tác mà trình thông dịch Python đang thực thi. Chi tiết về cách bạn có thể thực thi Python từ dòng lệnh của mình không quá quan trọng đối với mục đích của bài viết này, nhưng bạn có thể mở rộng hộp bên dưới để đọc thêm về sự khác biệt giữa các dòng lệnh trên Windows,

Môi trường dòng lệnh Hiển thị

Bây giờ bạn nên thực thi 

$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
9tập lệnh từ dòng lệnh, như hình dưới đây

$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'

Trong ví dụ này, bạn có thể thấy rằng 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5có giá trị 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
2, trong đó các ký hiệu trích dẫn [ 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
3] cho bạn biết rằng giá trị có kiểu chuỗi

Hãy nhớ rằng, trong Python, không có sự khác biệt giữa các chuỗi được xác định bằng dấu nháy đơn [ 

 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
3] và dấu nháy kép [ 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
5]. Bạn có thể đọc thêm về cách xác định chuỗi trong

Bạn sẽ thấy đầu ra giống nhau nếu bạn bao gồm một dòng shebang trong tập lệnh của mình và thực thi nó trực tiếp [ 

 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
6] hoặc sử dụng 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
7phép thuật trong IPython hoặc Jupyter Notebooks

Bạn cũng có thể thấy các lệnh Python được thực thi từ bên trong các gói bằng cách bổ sung 

 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
8đối số vào lệnh. Thông thường, bạn sẽ thấy điều này được đề cập khi bạn đang sử dụng 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
9.
$ python3 best_practices.py
This is my file to demonstrate best practices.
0

Thêm 

 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
8đối số sẽ chạy mã trong 
$ python3 best_practices.py
This is my file to demonstrate best practices.
2mô-đun của một gói. Bạn có thể tìm thêm thông tin về 
$ python3 best_practices.py
This is my file to demonstrate best practices.
2tệp trong

Trong cả ba trường hợp này, 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5không có cùng giá trị. chuỗi 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
2

Chi tiết kỹ thuật. Tài liệu Python được xác định cụ thể khi nào 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5 sẽ có giá trị 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
2

Mô-đun 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5được đặt bằng 
 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data
2khi đọc từ đầu vào chuẩn, tập lệnh hoặc từ lời nhắc tương tác. [  Nguồn ]

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5được lưu trữ trong không gian tên toàn cầu của các mô-đun giống nhau với 
>>> import best_practices
This is my file to demonstrate best practices.
1, 
>>> import best_practices
This is my file to demonstrate best practices.
2và các thuộc tính khác. Bạn có thể đọc thêm về các thuộc tính này trong tài liệu Mô hình dữ liệu Python và, đặc biệt cho các mô-đun và gói, trong

Nhập vào Mô-đun hoặc Trình thông tin tương tác

Bây giờ chúng ta hãy xem xét cách thứ hai mà trình thông dịch Python sẽ thực thi mã của bạn. Import. Khi bạn đang phát triển một mô-đun hoặc tập lệnh, rất có thể bạn sẽ muốn tận dụng các mô-đun mà người khác đã xây dựng, điều này bạn có thể thực hiện với 

>>> import best_practices
This is my file to demonstrate best practices.
3từ khóa

Trong quá trình nhập, Python thực thi các câu lệnh đã được xác định trong mô-đun chỉ được xác định [nhưng chỉ trong lần đầu tiên bạn nhập một mô-đun]. Để chứng minh kết quả nhập 

$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
9tệp của bạn, hãy khởi động trình thông dịch Python tương tác và sau đó nhập 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
9tệp của bạn

>>>

>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'

Trong phần đầu mã này, bạn có thể thấy rằng trình thông dịch Python thực thi lệnh gọi tới 

>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0. Hai dòng đầu tiên của đầu ra giống như khi bạn thực thi tệp dưới dạng tệp lệnh trên dòng lệnh vì không có biến nào trong một trong hai dòng đầu tiên. Tuy nhiên, có sự khác biệt về sản lượng so với thứ ba 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0

Khi mã giao dịch Python input code, value of _____3_______5được đặt giống với tên của mô-đun đang được nhập. You can see this thing in the first line out on the third.

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5có giá trị 
11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
0, là tên của 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
9tệp mà Python đang nhập từ đó

Lưu ý rằng nếu bạn 

>>> import best_practices
This is my file to demonstrate best practices.
3trả lại mô-đun mà không thoát khỏi Python, thì sẽ không có đầu ra

Lưu ý. Để biết thêm thông tin về cách nhập hoạt động trong Python, hãy xem bản Python. Kỹ thuật và mẹo cao cũng như Nhập tuyệt đối so với đối tương trong Python

Các phương pháp hay nhất cho các hàm chính của Python

Bây giờ bạn có thể thấy sự khác biệt trong cách Python xử lý các chế độ thực thi khác nhau của nó, điều này rất hữu ích cho bạn khi biết một số phương pháp hay nhất để sử dụng. Những điều này sẽ được áp dụng bất kể khi nào bạn muốn viết mã mà bạn có thể chạy dưới dạng tệp lệnh và nhập vào một mô-đun khác hoặc một phiên bản tương tác

Bạn sẽ tìm hiểu về bốn phương pháp hay nhất để chắc chắn rằng mã của bạn có thể phục vụ một mục tiêu kép

  1. Đặt hầu hết mã vào một hàm hoặc lớp
  2. Sử dụng 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    5 để kiểm tra giám sát việc thực thi mã của bạn
  3. Tạo một hàm được gọi là 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4 để chứa mã bạn muốn chạy
  4. Gọi các chức năng khác nhau từ 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4

Đặt hầu hết mã vào một hàm hoặc lớp

Hãy nhớ rằng trình thông dịch Python thực thi tất cả các mã trong một mô-đun khi nó nhập mô-đun. Đôi khi mã bạn viết sẽ có các tác dụng phụ mà bạn muốn người dùng kiểm tra, chẳng hạn như

  • Chạy một tính toán mất nhiều thời gian
  • Ghi vào tệp trên đĩa
  • Trong thông tin có thể làm lộn ngược thiết bị đầu cuối của người dùng

Trong những trường hợp này, bạn muốn người dùng kiểm tra việc kích hoạt việc thực thi mã này, thay vì để trình thông dịch Python thực thi mã khi nó nhập mô-đun của bạn

Do đó, cách tốt nhất là  né hầu hết mã vào bên trong một hàm hoặc một lớp. Điều này là làm khi trình thông dịch Python gặp 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
6hoặc 
11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
7từ khóa, nó chỉ lưu trữ các định nghĩa để sử dụng sau này và không thực sự thi cho đến khi bạn yêu cầu

Lưu đoạn mã dưới đây vào một tệp được gọi là 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8 để có thể hiện ý tưởng này

 1from time import sleep
 2
 3print["This is my file to demonstrate best practices."]
 4
 5def process_data[data]:
 6    print["Beginning data processing..."]
 7    modified_data = data + " that has been modified"
 8    sleep[3]
 9    print["Data processing finished."]
10    return modified_data

Trong mã này, trước tiên bạn nhập 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
9từ 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
0mô-đun

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
9 tạm dừng trình thông dịch trong bao nhiêu giây mà bạn đưa ra làm đối số và sẽ tạo ra một hàm mất nhiều thời gian để chạy cho ví dụ này. Tiếp theo, bạn sử dụng 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0 để trong một câu mô tả đích của đoạn mã này

Sau đó, bạn xác định một chức năng có tên 

$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3 năm thực hiện công việc

  1. Trong một số đầu ra để người dùng biết rằng quá trình xử lý dữ liệu đang bắt đầu
  2. Sửa đổi dữ liệu đầu vào
  3. Tạm dừng việc thực thi trong ba giây bằng cách sử dụng 
    11if __name__ == "__main__":
    12    data = "My data read from the Web"
    13    print[data]
    14    modified_data = process_data[data]
    15    print[modified_data]
    
    9
  4. Trong một số đầu ra để cho người dùng biết rằng quá trình xử lý đã hoàn tất
  5. Trả về dữ liệu đã sửa đổi

Thực thi tập tin phương pháp hay nhất trên dòng lệnh

Bây giờ, điều gì sẽ xảy ra khi bạn thực thi tệp này dưới dạng tệp lệnh trên dòng lệnh?

Trình thông dịch Python sẽ thực thi các dòng 

$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
5và 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0dòng nằm ngoài định nghĩa của hàm, sau đó nó sẽ tạo ra định nghĩa của hàm được gọi là 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3. Sau đó, tập lệnh sẽ thoát ra mà không cần làm gì thêm, vì tập lệnh không có bất kỳ mã nào thực thi _______74_______3

Khối mã bên dưới hiển thị kết quả của việc chạy tệp này dưới dạng tệp lệnh

$ python3 best_practices.py
This is my file to demonstrate best practices.

Đầu ra mà chúng ta có thể thấy ở đây là kết quả của lần đầu tiên 

>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0. Lưu ý rằng nhập từ 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
0và xác định 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3không tạo ra đầu ra. Cụ thể, kết quả đầu ra của các lệnh gọi đến 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0bên trong định nghĩa của 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3 sẽ không được đưa ra

Nhập Mô-đun Các phương pháp hay nhất trong mô-đun khác hoặc Trình thông tin tương tác

Khi bạn nhập tệp này trong một phiên bản tương tác [hoặc một mô-đun khác], trình dịch Python sẽ thực hiện chính xác các bước giống như khi thực hiện tệp này dưới dạng tệp lệnh

Sau khi trình thông dịch Python nhập tệp, bạn có thể sử dụng bất kỳ biến, lớp hoặc hàm nào được xác định trong mô-đun bạn đã nhập. Để chứng minh điều này, chúng tôi sẽ sử dụng trình thông dịch Python tương tác. Khởi động chương trình tương tác thông tin và sau đó nhập 

>>> import best_practices
This is my file to demonstrate best practices.
4

>>>

>>> import best_practices
This is my file to demonstrate best practices.

Đầu ra duy nhất từ ​​công việc đầu vào 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tệp là từ lệnh 
>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0 call đầu tiên được xác định bên ngoài 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3. Nhập từ 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
0và xác định 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3không tạo đầu ra, giống như khi bạn thực thi mã từ dòng lệnh

Sử dụng 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
00 để kiểm tra việc thực thi mã của bạn

Điều gì sẽ xảy ra nếu bạn muốn _______74_______3 thực thi khi bạn chạy tập lệnh từ dòng lệnh nhưng không phải khi trình dịch Python nhập tệp?

Bạn có thể sử dụng 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
00thành ngữ để xác định ngữ cảnh thực thi và 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3 chỉ chạy có điều kiện khi 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5 bằng 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
3. Add the bottom code into the end____63_______8tep của bạn

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]

Trong mã này, bạn đã thêm một câu lệnh điều kiện để kiểm tra giá trị của 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5. Điều kiện này sẽ đánh giá 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
5khi nào 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5 bằng chuỗi 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
3. Hãy nhớ rằng các giá trị đặc biệt của 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
3cho 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5 biến có nghĩa là thông dịch Python được thực hiện kịch bản của bạn và không nhập nó

Bên trong điều kiện khối, bạn đã thêm bốn dòng mã [dòng 12, 13, 14 và 15]

  • Dòng 12 và 13. Bạn đang tạo một biến 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    13 lưu trữ dữ liệu bạn có được từ Web và trong đó
  • Dòng 14. You are doing data
  • Dòng 15. You are in the data used to change

Bây giờ, hãy chạy 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8filelệnh của bạn từ dòng lệnh để xem kết quả đầu ra sẽ thay đổi như thế nào

$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified

Đầu tiên, đầu ra hiển thị kết quả của 

>>> import execution_methods
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: 'execution_methods'
0 cuộc gọi bên ngoài 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3

Sau đó, giá trị của 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
13được đưa ra. Điều này xảy ra là do biến 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5có giá trị 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
3khi trình thông dịch Python thực thi tệp này dưới dạng tệp lệnh, vì vậy câu lệnh điều kiện được đánh giá là 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
5

Tiếp theo, tập lệnh của bạn được gọi là 

$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3và chuyển 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
13vào để sửa đổi. Khi ___74_______3 thực thi, nó sẽ ở một số thông báo trạng thái đến đầu ra. Cuối cùng, giá trị của 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
24được in ra

Bây giờ bạn nên kiểm tra xem điều gì sẽ xảy ra khi bạn nhập 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tệp từ trình thông tin tương tác [hoặc một mô-đun khác]. Ví dụ dưới đây minh họa sự cố này

>>>

>>> import best_practices
This is my file to demonstrate best practices.

Lưu ý rằng bạn nhận được hành vi giống như trước khi bạn thêm câu lệnh điều kiện vào cuối tệp. Điều này là do 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5 biến có giá trị 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
27, vì vậy Python đã không thực thi mã bên trong khối, bao gồm 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3, bởi vì lệnh điều kiện được đánh giá là 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
29

Tạo một hàm được gọi là hàm chính [] để chứa mã bạn muốn chạy

Bây giờ bạn có thể viết mã Python có thể chạy từ dòng lệnh dưới dạng tệp lệnh và được nhập mà không có tác dụng phụ không mong muốn. Tiếp theo, bạn sẽ tìm hiểu về cách viết mã của mình để giúp các lập trình viên Python khác dễ dàng hơn theo ý bạn

Nhiều ngôn ngữ, chẳng hạn như C , C ++ , Java và một số ngôn ngữ khác, xác định một hàm đặc biệt phải được gọi là 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 mà hệ điều hành tự động gọi khi nó thực hiện biên dịch chương trình này. Hàm này thường được gọi là điểm vào bởi vì nó là nơi thực thi đi vào chương trình

Ngược lại, Python không có chức năng đặc biệt nào đóng vai trò là điểm đến của một lệnh tập tin. Bạn thực sự có thể đặt cho điểm đầu vào hàm trong một tập lệnh Python bất kỳ tên nào bạn muốn

Mặc dù Python không gán bất kỳ ý nghĩa nào cho một hàm được đặt tên 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4, nhưng cách tốt nhất là đặt tên cho hàm điểm đầu vào
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4. Bằng cách đó, bất kỳ lập trình viên nào khác khi đọc tập lệnh của bạn đều biết rằng chức năng này là điểm bắt đầu của mã hoàn thành nhiệm vụ chính của tập lệnh

Ngoài ra, 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 phải chứa bất kỳ mã nào bạn muốn chạy khi trình thông dịch Python thực thi tệp này. Điều này tốt hơn là đặt mã trực tiếp vào khối có điều kiện vì người dùng có thể sử dụng lại 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 nếu họ nhập mô-đun của bạn

Thay đổi 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tep để file trông giống như mã bên dưới

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
0

Trong ví dụ này, bạn đã thêm định nghĩa của 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 điều kiện bao gồm mã trước đó nằm bên trong khối điều kiện. Sau đó, bạn đã thay đổi điều kiện khối để nó thực thi 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4. Nếu bạn chạy mã này dưới dạng tệp lệnh hoặc nhập nó, bạn sẽ nhận được đầu giống như trong phần trước

Gọi các chức năng khác từ main[]

Một thực tế phổ biến bằng Python là có 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 thực hiện các chức năng khác chứ không phải là bao gồm cả mã nhiệm vụ hoàn thành trong 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4. Điều này đặc biệt hữu ích khi bạn có thể tạo ra nhiệm vụ tổng thể của mình từ một số nhiệm vụ con nhỏ hơn là có thể thực hiện chức vụ độc lập

Ví dụ. you could have an a command doing the following task

  1. Đọc tệp dữ liệu từ một nguồn có thể là cơ sở dữ liệu, tệp trên đĩa hoặc web API
  2. Xử lý dữ liệu
  3. Ghi dữ liệu đã xử lý đến một vị trí khác

Nếu bạn triển khai từng nhiệm vụ này trong các chức năng riêng biệt, thì bạn [hoặc người dùng khác] sẽ dễ dàng sử dụng lại một vài bước trong số các bước và bỏ qua các bước bạn không muốn. Sau đó, bạn có thể tạo ra một quy trình làm việc mặc định trong đó 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4và bạn có thể có cả hai thế giới tốt nhất

Việc áp dụng phương pháp này cho mã của bạn hay không phải do bạn tự đánh giá. Việc chia nhỏ công việc thành nhiều chức năng giúp việc sử dụng lại dễ dàng hơn nhưng lại làm tăng khó khăn cho những người khác đang cố gắng diễn giải mã của bạn vì họ phải thực hiện theo một số bước nhảy trong quy trình của chương trình

Sửa đổi 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tep của bạn để tệp trông giống như mã bên dưới

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
1

Trong ví dụ mã này, 10 dòng đầu tiên của tệp có cùng nội dung mà chúng đã có trước đó. Định nghĩa hàm thứ hai trên dòng 12 tạo và trả về một số dữ liệu mẫu và định nghĩa hàm thứ ba trên dòng 17 mô phỏng việc ghi dữ liệu đã được sửa đổi vào cơ sở dữ liệu

Ở dòng 21, 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4đã được xác định. Trong ví dụ này, bạn đã sửa đổi 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 để nó gọi lần lượt các chức năng đọc dữ liệu, xử lý dữ liệu và ghi dữ liệu

Đầu tiên, cái 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
13được tạo ra từ 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
45. Điều này 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
13được chuyển đến 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3, được trả lại 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
24. Cuối cùng, 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
24được chuyển vào 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
50

Hai dòng cuối cùng của câu lệnh là khối điều kiện kiểm tra 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
5và chạy 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4 if 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
1câu lệnh là 
$ python3 execution_methods.py
This is my file to test Python's execution methods.
The variable __name__ tells me which context this file is running in.
The value of __name__ is: '__main__'
5

Bây giờ, bạn có thể chạy toàn bộ quy trình xử lý từ dòng lệnh, như được hiển thị bên dưới

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
2

Khi bắt đầu thực hiện điều này, bạn có thể thấy rằng trình thông dịch Python thực thi 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4, trong đó thực hiện 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
45, 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3và 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
50. Tuy nhiên, bạn cũng có thể nhập 
11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tệp và sử dụng lại 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3cho một nguồn dữ liệu đầu vào khác, như được hiển thị bên dưới

>>>

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
3

Trong ví dụ này, bạn đã nhập 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
61và rút ngắn tên 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
62cho mã này

Quá trình nhập làm cho trình thông dịch Python thực thi tất cả các dòng mã trong 

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tệp, hãy làm điều đó, bắt đầu hiển thị dòng giải thích đích của tệp

Sau đó, bạn lưu trữ dữ liệu từ một tệp trong 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
13đó là do đọc dữ liệu từ Web. Sau đó, bạn sử dụng lại 
$ python3 best_practices.py
This is my file to demonstrate best practices.
My data read from the Web
Beginning data processing...
Data processing finished.
My data read from the Web that has been modified
3và 
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
50từ 
11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print[data]
14    modified_data = process_data[data]
15    print[modified_data]
8tép. Trong trường hợp này, bạn đã tận dụng lợi thế của công việc bằng cách sử dụng lại mã của mình thay vì xác định tất cả logic trong
print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4

Tóm tắt các phương pháp hay nhất về hàm chính trong Python

Dưới đây là bốn phương pháp hay nhất chính về 

print["This is my file to test Python's execution methods."]
print["The variable __name__ tells me which context this file is running in."]
print["The value of __name__ is:", repr[__name__]]
4Python mà bạn vừa xem

  1. Đặt mã mất nhiều thời gian để chạy hoặc có các hiệu ứng khác trên máy tính vào một hàm hoặc lớp để bạn có thể kiểm tra chính xác thời điểm mã đã được thực thi

  2. Sử dụng các giá trị khác nhau của 

    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    5 để xác định ngữ cảnh và thay đổi hành vi của mã của bạn bằng một câu lệnh điều kiện

  3. Bạn nên đặt tên cho hàm điểm đầu vào của mình 

    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4 để truyền đạt ý định của hàm, mặc dù Python không gán bất kỳ ý nghĩa đặc biệt nào cho một hàm được đặt tên 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4

  4. Nếu bạn muốn sử dụng lại chức năng từ mã của mình, hãy xác định logic trong các hàm bên ngoài 

    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4và gọi các hàm đó bên trong 
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    4

Phần kết luận

Xin chúc mừng. Bây giờ bạn đã biết cách tạo các _______3_______4hàm Python

Bạn đã học được những điều sau

  • Biết giá trị của 

    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    5 biến là điều kiện quan trọng để viết mã phục vụ đích kép của lệnh thi thực thi và mô-đun có thể nhập

  • print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    5nhận các giá trị khác nhau tùy thuộc vào cách bạn thực thi tệp Python của mình.
    print["This is my file to test Python's execution methods."]
    print["The variable __name__ tells me which context this file is running in."]
    print["The value of __name__ is:", repr[__name__]]
    
    5 sẽ bằng

Chủ Đề