Hướng dẫn how to call functions in main python - cách gọi hàm trong python chính

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Xác định các chức năng chính trong Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Defining Main Functions in Python

Nhiều ngôn ngữ lập trình có một chức năng đặc biệt được tự động thực hiện khi một hệ đ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__))
6 và phải có một loại 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 tập lệnh bắt đầu từ đầu tệp 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 được xác định để thực hiện một chương trình là hữu ích để hiểu cách thức hoạt động của một chương trình. Các lập trình viên Python đã đưa ra một số quy ước để xác định điểm xuất phát này.

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

  • 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__))
    
    7 đặc biệt là gì và Python định nghĩa nó như thế nào
  • Tại sao bạn muốn 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__))
    
    6 trong 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__))
    
    6 trong Python
  • Thực hành tốt nhất là gì cho mã nào để đặt 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__))
    
    6 của bạn

Một Python chính cơ bản ()

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

def main():
    print("Hello World!")

if __name__ == "__main__":
    main()

Trong mã này, có một hàm 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__))
6 in cụm từ
eleanor@realpython:~/Documents$
2 khi trình thông dịch Python thực thi nó. Ngoài ra còn có một câu lệnh có điều kiện (hoặc
eleanor@realpython:~/Documents$
3) 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__))
7 và so sánh nó với chuỗi
eleanor@realpython:~/Documents$
5. Khi câu lệnh
eleanor@realpython:~/Documents$
3 đánh giá thành
eleanor@realpython:~/Documents$
7, 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__))
6. Bạn có thể đọc thêm về các tuyên bố có điều kiện trong các tuyên bố có đ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 thi dưới dạng tập lệnh và được nhập trong một mô -đun khác. Để giúp hiểu mã này sẽ thực thi như thế nào, 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__))
7 tùy thuộc vào cách thực hiện mã.executed as a script and imported in another module. To help understand how this code will execute, you should first understand how the Python interpreter sets
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__))
7 depending on how the code is being executed.

Chế độ thực thi trong 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 bằng dòng lệnh.script using the command line.
  2. Bạn có thể nhập mã từ một tệp Python vào một tệp khác hoặc vào trình thông dịch tương tác.import the code from one Python file into another file or into the interactive interpreter.

Bạn có thể đọc nhiều hơn về các phương pháp này trong cách chạy các kịch bản Python của bạn. Bất kể cách chạy mã của bạn mà bạn sử dụng, Python định nghĩa một biến đặc biệt có 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__))
7 có chứa một chuỗi có giá trị phụ thuộc vào cách sử dụng mã.

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

C:\Users\Eleanor\Documents>
1, để khám phá cách hành vi 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 cuộc gọi đến

C:\Users\Eleanor\Documents>
2 được xác định. Hai bản in đầu tiên một số cụm từ giới thiệu.
C:\Users\Eleanor\Documents>
2 thứ ba trước tiên sẽ in cụm từ
C:\Users\Eleanor\Documents>
4, và sau đó nó sẽ in biểu diễn của 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__))
7 bằng cách sử dụng Python, tích hợp
C:\Users\Eleanor\Documents>
6.

Trong Python,

C:\Users\Eleanor\Documents>
6 hiển thị biểu diễn có thể in của một đối tượng. Ví dụ này sử dụng
C:\Users\Eleanor\Documents>
6 để 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__))
7 là một chuỗi. Bạn có thể đọc thêm về
C:\Users\Eleanor\Documents>
6 trong tài liệu Python.

Bạn sẽ thấy tệp từ, mô -đun và tập lệnh được sử dụng trong suốt bài viết này. Trên thực tế, có nhiều sự khác biệt giữa họ. Tuy nhiên, có sự khác biệt nhỏ về ý nghĩa nhấn mạnh mục đích của một đoạn mã:file, module, and script used throughout this article. Practically, there isn’t much difference between them. However, there are slight differences in meaning that emphasize the purpose of a piece of code:

  1. Tệp: Thông thường, tệp Python là bất kỳ tệp nào chứa mã. Hầu hết các tệp Python có phần mở rộ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__'
    
    1. Typically, a Python file is any file that contains code. Most Python files have the extension
    $ 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__'
    
    1.

  2. Tập lệnh: Tập lệnh Python là một tệp mà bạn dự định thực thi từ dòng lệnh để hoàn thành một tác vụ. A Python script is a file that you intend to execute from the command line to accomplish a task.

  3. Mô -đun: Mô -đun Python là một tệp mà bạn dự định nhập từ trong một mô -đun khác hoặc tập lệnh hoặc từ trình thông dịch tương tác. Bạn có thể đọc thêm về các mô -đun trong các mô -đun và gói Python - Giới thiệu. A Python module is a file that you intend to import from within another module or a script, or from the interactive interpreter. You can read more about modules in Python Modules and Packages – An Introduction.

Sự khác biệt này cũng được thảo luận trong cách chạy các kịch bản 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 hiện tập lệnh Python của mình từ dòng lệnh.

Khi bạn thực thi một tập lệnh, bạn sẽ không thể xác định tương tác mã mà trình thông dịch Python đang thực thi. Các chi tiết về cách bạn có thể thực hiện Python từ dòng lệnh của mình không quan trọng cho 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 dòng lệnh trên Windows, Linux và MacOS.

Cách bạn bảo máy tính thực thi mã từ dòng lệnh hơi khác nhau tùy thuộc vào hệ điều hành của bạn.

Trên Linux và MacOS, dòng lệnh thường trông giống như ví dụ dưới đây:

eleanor@realpython:~/Documents$

Phần trước dấu hiệu đô la (

$ 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__'
2) có thể trông khác nhau, tùy thuộc vào tên người dùng của bạn và tên máy tính của bạn. Các lệnh mà bạn gõ sẽ theo đuổ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__'
2. Trên Linux hoặc MacOS, tên của Python 3 thực thi 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__'
4, vì vậy bạn nên chạy các tập lệnh Python bằng cách 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__'
5 sau
$ 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__'
2.

Trên Windows, lời nhắc lệnh thường trông giống như ví dụ dưới đây:

C:\Users\Eleanor\Documents>

Phần trước

$ 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__'
7 có thể trông khác nhau, tùy thuộc vào tên người dùng của bạn. Các lệnh mà bạn gõ sẽ theo đuổ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__'
7. Trên Windows, tên của Python 3 thực thi thường 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__'
9, vì vậy bạn nên chạy các tập lệnh Python bằng cách nhập
>>> 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 sau
$ 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__'
7.

Bất kể hệ điều hành của bạn là gì, đầu ra từ các tập lệnh Python mà bạn sử dụng trong bài viết này sẽ giống nhau, vì vậy chỉ có kiểu đầu vào Linux và MacOS được hiển thị trong bài viết này và dòng đầu vào sẽ bắt đầu tạ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__'
2.

Bây giờ bạn nên thực thi tập lệnh

C:\Users\Eleanor\Documents>
1 từ dòng lệnh, như được hiển thị bên dướ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__'

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__))
7 có giá trị
>>> 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'
5, trong đó các ký hiệu báo giá (
>>> 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'
6) cho bạn biết rằng giá trị có loại 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 các trích dẫn đơ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'
6) và trích dẫn kép (
>>> 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'
8). Bạn có thể đọc thêm về việc xác định chuỗi trong các loại dữ liệu cơ bản trong Python.

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

>>> 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) hoặc sử dụng ma thuật
 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
0 trong ipython hoặc máy tính xách tay Jupyter.

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

 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
1 vào lệnh. Thông thường, bạn sẽ thấy điều này được đề xuất khi bạn 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
2:
 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.

Thêm đối số

 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
1 chạy mã trong mô -đun
 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 của gói. Bạn có thể tìm thêm thông tin về tệ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 trong cách xuất bản gói Python nguồn mở lên PYPI.

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__))
7 có cùng giá trị: chuỗ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'
5.

Nhập vào một mô -đun hoặc trình thông dịch tương tác

Bây giờ, hãy để một cách nhìn vào cách thứ hai mà trình thông dịch Python sẽ thực thi mã của bạn: nhập. 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 mà bạn có thể làm với từ khóa

 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.

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

C:\Users\Eleanor\Documents>
1 của bạn, hãy khởi động trình thông dịch Python tương tác và sau đó nhập tệp
C:\Users\Eleanor\Documents>
1 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 đầu ra mã này, bạn có thể thấy rằng trình thông dịch Python thực hiện ba cuộc gọi đến

C:\Users\Eleanor\Documents>
2. Hai dòng đầu ra đầu tiên giống hệt 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ó một sự khác biệt về đầu ra từ
C:\Users\Eleanor\Documents>
2 thứ ba.

Khi trình thông dịch Python nhập mã, 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__))
7 được đặt giống như tên của mô -đun đang được nhập. Bạn có thể thấy điều này trong dòng thứ ba của đầu ra ở trê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__))
7 có giá trị
$ python3 best_practices.py
This is my file to demonstrate best practices.
6, là tên của tệ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__'
1 mà Python đang nhập từ.

Lưu ý rằng nếu bạ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
9 lại mô -đun mà không bỏ Python, sẽ không có đầu ra.

Thực tiễn tốt nhất cho các chức năng 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ó, nó rất hữu ích cho bạn để biết một số thực tiễn tốt nhất để sử dụng. Chúng sẽ được áp dụng bất cứ 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 trong một mô -đun khác hoặc phiên tương tác.

Bạn sẽ tìm hiểu về bốn thực tiễn tốt nhất để đảm bảo rằng mã của bạn có thể phục vụ mục đích kép:

  1. Đặt hầu hết các mã vào một chức năng 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__))
    
    7 để kiểm soát việc thực thi mã của bạn.
  3. Tạo một chức năng 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__))
    
    6 để chứa mã bạn muốn chạy.
  4. Gọi các chức năng khác 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__))
    
    6.

Đặt hầu hết các mã vào một chức năng hoặc lớp học

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 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 soát, chẳng hạn như:

  • Chạy một tính toán mất nhiều thời gian
  • Ghi vào một tệp trên đĩa
  • In thông tin sẽ làm lộn xộn 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 soát 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 nhập mô -đun của bạn.

Do đó, thực tiễn tốt nhất là bao gồm hầu hết các mã bên trong một hàm hoặc một lớp. Điều này là do khi trình thông dịch Python gặp phải từ khóa

>>> import best_practices
This is my file to demonstrate best practices.
2 hoặc
>>> import best_practices
This is my file to demonstrate best practices.
3, nó chỉ lưu trữ các định nghĩa đó để sử dụng sau này và thực sự thực hiện chúng cho đến khi bạn nói với nó.include most code inside a function or a class. This is because when the Python interpreter encounters the
>>> import best_practices
This is my file to demonstrate best practices.
2 or
>>> import best_practices
This is my file to demonstrate best practices.
3 keywords, it only stores those definitions for later use and doesn’t actually execute them until you tell it to.

Lưu mã bên dưới vào một tệp có tên

>>> import best_practices
This is my file to demonstrate best practices.
4 để chứng minh ý 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

>>> import best_practices
This is my file to demonstrate best practices.
5 từ mô -đun
>>> import best_practices
This is my file to demonstrate best practices.
6.

>>> import best_practices
This is my file to demonstrate best practices.
5 tạm dừng trình thông dịch trong nhiều giây bạn đưa ra như một đối số và sẽ tạo ra một chức năng mất nhiều thời gian để chạy cho ví dụ này. Tiếp theo, bạn sử dụng
C:\Users\Eleanor\Documents>
2 để in một câu mô tả mục đích của mã này.

Sau đó, bạn xác định một hàm gọi là

>>> import best_practices
This is my file to demonstrate best practices.
9 thực hiện năm điều:

  1. In một số đầu ra để nói với người dùng rằng việc 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 hiện trong ba giây bằng cách sử dụng
    >>> import best_practices
    This is my file to demonstrate best practices.
    
    5
  4. In một số đầu ra để nói với người dùng rằng việc xử lý đã hoàn thành
  5. Trả về dữ liệu đã sửa đổi

Thực hiện tệp thực hành tốt 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

11if __name__ == "__main__":
12    data = "My data read from the Web"
13    print(data)
14    modified_data = process_data(data)
15    print(modified_data)
1 và
C:\Users\Eleanor\Documents>
2 nằm ngoài định nghĩa hàm, sau đó nó sẽ tạo định nghĩa của hàm gọi là
>>> import best_practices
This is my file to demonstrate best practices.
9. Sau đó, tập lệnh sẽ thoát mà không làm gì nữa, vì tập lệnh không có bất kỳ mã nào thực thi
>>> import best_practices
This is my file to demonstrate best practices.
9.

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

C:\Users\Eleanor\Documents>
2 đầu tiên. Lưu ý rằng nhập từ
>>> import best_practices
This is my file to demonstrate best practices.
6 và xác định
>>> import best_practices
This is my file to demonstrate best practices.
9 không tạo ra đầu ra. Cụ thể, các đầu ra của các cuộc gọi đến
C:\Users\Eleanor\Documents>
2 nằm trong định nghĩa của
>>> import best_practices
This is my file to demonstrate best practices.
9 không được in!

Nhập tệp thực hành tốt nhất trong một mô -đun khác hoặc trình thông dịch tương tác

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

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 chức năng nào được xác định trong mô -đun mà 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. Bắt đầu trình thông dịch tương tác và sau đó 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__))
00:

>>>

________số 8

Đầu ra duy nhất từ ​​việc nhập tệp

>>> import best_practices
This is my file to demonstrate best practices.
4 là từ cuộc gọi
C:\Users\Eleanor\Documents>
2 đầu tiên được xác định bên ngoài
>>> import best_practices
This is my file to demonstrate best practices.
9. Nhập từ
>>> import best_practices
This is my file to demonstrate best practices.
6 và xác định
>>> import best_practices
This is my file to demonstrate best practices.
9 không tạo ra đầ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__)) 06 để kiểm soát việc thực thi mã của bạn

Điều gì sẽ xảy ra nếu bạn muốn

>>> import best_practices
This is my file to demonstrate best practices.
9 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 thông dịch Python nhập tệp?

Bạn có thể sử dụng thành 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__))
06 để xác định bối cảnh thực thi và chỉ chạy có điều kiện
>>> import best_practices
This is my file to demonstrate best practices.
9 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__))
7 bằng
eleanor@realpython:~/Documents$
5. Thêm mã bên dưới vào cuối tệp
>>> import best_practices
This is my file to demonstrate best practices.
4 của bạn:execution context and conditionally run
>>> import best_practices
This is my file to demonstrate best practices.
9 only when
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__))
7 is equal to
eleanor@realpython:~/Documents$
5. Add the code below to the bottom of your
>>> import best_practices
This is my file to demonstrate best practices.
4 file:

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 có đ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__))
7. Điều kiện này sẽ đánh giá đến
eleanor@realpython:~/Documents$
7 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__))
7 bằng với chuỗi
eleanor@realpython:~/Documents$
5. Hãy nhớ rằng giá trị đặc biệt của
eleanor@realpython:~/Documents$
5 cho 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__))
7 có nghĩa là trình thông dịch Python đang thực thi tập lệnh của bạn và không nhập nó.

Bên trong khối có điều kiện, 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__))
    
    19 lưu trữ dữ liệu mà bạn đã thu được từ web và in nó.
    You are creating a variable
    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__))
    
    19 that stores the data you’ve acquired from the Web and printing it.
  • Dòng 14: Bạn đang xử lý dữ liệu. You are processing the data.
  • Dòng 15: Bạn đang in dữ liệu sửa đổi. You are printing the modified data.

Bây giờ, hãy chạy tập lệnh

>>> import best_practices
This is my file to demonstrate best practices.
4 của bạn từ dòng lệnh để xem đầu ra sẽ thay đổi như thế 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__))
0

Đầu tiên, đầu ra cho thấy kết quả của cuộc gọi

C:\Users\Eleanor\Documents>
2 bên ngoài
>>> import best_practices
This is my file to demonstrate best practices.
9.

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__))
19 được in. Điều này đã xảy ra vì 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__))
7 có giá trị
eleanor@realpython:~/Documents$
5 khi trình thông dịch Python thực thi tệp dưới dạng tập lệnh, do đó câu lệnh có điều kiện được đánh giá là
eleanor@realpython:~/Documents$
7.

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

>>> import best_practices
This is my file to demonstrate best practices.
9 và thông qua
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__))
19 để sửa đổi. Khi
>>> import best_practices
This is my file to demonstrate best practices.
9 thực thi, nó in một số thông báo trạng thái ra đầ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__))
30 được in.

Bây giờ bạn nên kiểm tra những gì xảy ra khi bạn nhập tệp

>>> import best_practices
This is my file to demonstrate best practices.
4 từ trình thông dịch tương tác (hoặc mô -đun khác). Ví dụ dưới đây cho thấy tình huống này:

>>>

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

________số 8

Tạo một hàm gọi là main () để chứa mã bạn muốn chạy

Bây giờ bạn có thể viết mã Python có thể được 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 làm theo những gì bạn muốn nói.

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__))
6 mà hệ điều hành sẽ tự động gọi khi thực thi chương trình được biên dịch. Hàm này thường được gọi là điểm nhập vì đó là nơi thực hiện vào chương trình.entry point because it is where execution enters the program.

Ngược lại, Python không có chức năng đặc biệt đóng vai trò là điểm nhập cảnh vào một tập lệnh. Bạn thực sự có thể cung cấp chức năng điểm nhập trong 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ê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__))
6, nhưng thực tế tốt nhất là đặt tên cho chức năng điểm 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__))
6 dù sao đi nữa. Bằng cách đó, bất kỳ lập trình viên nào khác đọc tập lệnh của bạn ngay lập tức biết rằng hàm này là điểm khởi đầu của mã hoàn thành nhiệm vụ chính của tập lệnh.name the entry point function
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__))
6
anyways. That way, any other programmers who read your script immediately know that this function is the starting point of the code that accomplishes the primary task of the script.

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__))
6 nên 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. Điều này tốt hơn so với việc đặ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__))
6 nếu họ nhập mô -đun của bạn.

Thay đổi tệp

>>> import best_practices
This is my file to demonstrate best practices.
4 để 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__))
2

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__))
6 bao gồm mã trước đây bên trong khối có điều kiện. Sau đó, bạn đã thay đổi khối có điều kiện để 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__))
6. 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 ra 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 khác trong 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__))
6 thực thi các chức năng khác, thay vì bao gồm mã thực hiện nhiệm vụ 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__))
6. Điều này đặc biệt hữu ích khi bạn có thể soạn thảo nhiệm vụ tổng thể của mình từ một số tác vụ nhỏ hơn có thể thực thi độc lập.have
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__))
6 execute other functions
, rather than including the task-accomplishing code in
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__))
6. This is especially useful when you can compose your overall task from several smaller sub-tasks that can execute independently.

Ví dụ: bạn có thể có một tập lệnh làm như sau:

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

Nếu bạn thực hiện từng tác vụ phụ này trong các chức năng riêng biệt, thì thật dễ dàng để bạn (hoặc người dùng khác) sử dụng lại một vài bước và bỏ qua các bước bạn không muốn. Sau đó, bạn có thể tạo một quy trình công 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__))
6 và bạn có thể có tốt nhất cả hai thế giới.

Có nên áp dụng thực tiễn này cho mã của bạn hay không là một cuộc gọi phán xét từ phía bạn. Chia công việc thành một số chức năng giúp tái sử dụng dễ dàng hơn nhưng làm tăng độ khó cho người khác đang cố gắng giải thích mã của bạn vì chúng phải theo dõi một số bước nhảy trong luồng của chương trình.

Sửa đổi tệp

>>> import best_practices
This is my file to demonstrate best practices.
4 của bạn để nó 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__))
3

Trong mã ví dụ này, 10 dòng đầu tiên của tệp có cùng nội dung mà chúng có trước đây. Định nghĩa chức năng 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 ghi dữ liệu đã sửa đổi vào cơ sở dữ liệu.

Trên 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__))
6 đượ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__))
6 để lần lượt gọi các chức năng đọc dữ liệu, xử lý dữ liệu và ghi dữ liệu.

Đầu tiê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__))
19 được tạo 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__))
51.
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__))
19 này được chuyển cho
>>> import best_practices
This is my file to demonstrate best practices.
9, trả 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__))
30. 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__))
30 đượ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__))
56.

Hai dòng cuối cùng của tập lệnh là khối có đ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__))
7 và 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__))
6 nếu câu lệnh
eleanor@realpython:~/Documents$
3 là
eleanor@realpython:~/Documents$
7.

Bây giờ, bạn có thể chạy toàn bộ đường ống 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__))
4

Trong đầu ra từ lần thực hiện 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__))
6, đã 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__))
51,
>>> import best_practices
This is my file to demonstrate best practices.
9 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__))
56. Tuy nhiên, bạn cũng có thể nhập tệp
>>> import best_practices
This is my file to demonstrate best practices.
4 và sử dụng lại
>>> import best_practices
This is my file to demonstrate best practices.
9 cho 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__))
5

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__))
67 và rút ngắn tên thà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__))
68 cho mã này.

Quá trình nhập khiến trình thông dịch Python thực thi tất cả các dòng mã trong tệp

>>> import best_practices
This is my file to demonstrate best practices.
4, do đó, đầu ra hiển thị dòng giải thích mục đí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__))
19 thay vì đọc dữ liệu từ web. Sau đó, bạn đã sử dụng lại
>>> import best_practices
This is my file to demonstrate best practices.
9 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__))
56 từ tệp
>>> import best_practices
This is my file to demonstrate best practices.
4. Trong trường hợp này, bạn đã tận dụng việc tái sử dụng 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__))
6.

Tóm tắt chức năng chính của Python

Dưới đây là bốn thực tiễn tốt nhất 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__))
6 trong Python mà bạn vừa thấy:

  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 trong một hàm hoặc lớp, do đó bạn có thể kiểm soát chính xác khi 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__))
    
    7 để xác định bối cảnh và thay đổi hành vi của mã của bạn bằng một câu lệnh có điều kiện.

  3. Bạn nên đặt tên cho chức năng điểm nhập 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__))
    
    6 để 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ê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__))
    
    6.

  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 chức năng 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__))
    
    6 và gọi các chức năng đó 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__))
    
    6.

Sự kết luận

Xin chúc mừng! Bây giờ bạn đã biết cách tạo các chức năng Python

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

Bạn đã học như sau:

  • Biết giá trị của 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__))
    
    7 rất quan trọng để ghi mã phục vụ mục đích kép của tập lệnh 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__))
    
    7 đảm nhận các giá trị khác nhau tùy thuộc vào cách bạn thực hiện 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__))
    
    7 sẽ bằng:

    • eleanor@realpython:~/Documents$
      
      5 Khi tệp được thực thi từ dòng lệnh hoặc vớ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__))
      
      86 (để thực thi tệp gó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
      
      5)
    • Tên của mô -đun, nếu mô -đun đang được nhập
  • Các lập trình viên Python đã phát triển một tập hợp các thực tiễn tốt để sử dụng khi bạn muốn phát triển mã có thể tái sử dụng.

Bây giờ bạn đã sẵn sàng để viết một số mã chức năng Python

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__))
6 tuyệt vời!

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Xác định các chức năng chính trong Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Defining Main Functions in Python

__ hàm __ chính trong Python là gì?

__main__ là tên của môi trường nơi chạy mã cấp cao nhất.Mã cấp cao nhất là mô-đun Python do người dùng chỉ định đầu tiên bắt đầu chạy.Đó là cấp độ hàng đầu vì nó nhập tất cả các mô-đun khác mà chương trình cần.Đôi khi, mã cấp cao nhất mã hóa được gọi là điểm nhập vào ứng dụng.the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It's “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.

Bạn có thể gọi các chức năng trong một chức năng Python không?

Trong Python, bất kỳ chức năng bằng văn bản nào cũng có thể được gọi bởi một hàm khác.Lưu ý rằng đây có thể là cách phá vỡ một vấn đề thanh lịch nhất thành các vấn đề nhỏ.any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems.