Bạn có thể chuyển đối số cho hàm chính python không?

Nhiều ngôn ngữ lập trình có chức năng đặc biệt được thực thi tự động khi hệ điều hành bắt đầu chạy 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ó kiểu trả về và đối số cụ thể 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 rất hữu ích để hiểu chương trình hoạt động như thế nào. Các 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

  • 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 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__]]
    
    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
  • Các phương pháp hay nhất dành cho mã nào sẽ được đưa 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

Tải xuống miễn phí. Nhận một chương mẫu từ Thủ thuật Python. Cuốn sách chỉ cho bạn các phương pháp hay nhất về 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 hàm chính Python cơ bản []

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

def main[]:
    print["Hello World!"]

if __name__ == "__main__":
    main[]

Trong mã này, có một hàm tên 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 ra 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 đ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 ước tính thành
eleanor@realpython:~/Documents$
7, 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__]]
6. Bạn có thể đọc thêm về các câu điều kiện trong Câu lệnh 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 vào 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 mã được thực thi

Loại bỏ các quảng cáo

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
  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

Bạn có thể đọc nhiều hơn 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ó tên 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__]]
7 chứ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

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__]]
61, để khám phá hành vi của mã thay đổi như thế nào 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

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__]]
62 được xác định. Hai cái đầu tiên in một số cụm từ giới thiệ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__]]
62 thứ ba trước tiên sẽ in cụ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__]]
64, 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
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__]]
66 tích hợp sẵn của Python

Trong 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__]]
66 hiển thị biểu diễn có thể in được của một đối tượng. Ví dụ này 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__]]
66 để 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ề
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__]]
66 trong

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. Thực tế, không có nhiều 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 đích của một đoạn mã

  1. Tập tin. 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 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__]]
    
    71

  2. Kịch bản. Tập lệnh Python là một tệp mà bạn định thực thi từ dòng lệnh để hoàn thành một tác vụ

  3. mô-đun. Mô-đun Python là tệp mà bạn định nhập từ bên trong 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 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

Theo cách tiếp cận này, bạn muốn thực thi 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. 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 dòng lệnh trên Windows, Linux và macOS

Môi trường dòng lệnhHiển thị/Ẩn

Cách bạn yêu cầu máy tính thực thi mã từ dòng lệnh hơi khác một chút 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 giống như ví dụ bên dưới

eleanor@realpython:~/Documents$

Phần trước ký hiệu đô la [

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__]]
72] có thể trông khác, tùy thuộc vào tên người dùng và tên máy tính của bạn. Các lệnh mà bạn gõ sẽ đi sau
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__]]
72. Trên Linux hoặc macOS, tên của tệp thực thi Python 3 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__]]
74, vì vậy bạn nên chạy các tập lệnh Python bằng cách 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__]]
75 sau
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__]]
72

Trên Windows, dấu nhắc lệnh thường giống như ví dụ 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__]]
6

Phần trướ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__]]
77 có thể trông khác, 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ẽ đi sau
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__]]
77. Trên Windows, tên của tệp thực thi Python 3 thường 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__]]
79, vì vậy bạn nên chạy các tập lệnh Python bằng cách 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__]]
80 sau
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__]]
77

Bất kể hệ điều hành của bạn là gì, kết quả đầ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, do đó, 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

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__]]
72

Bây giờ bạn nên thực thi tập lệ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__]]
61 từ dòng lệnh, như hình 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__]]
7

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ị
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__]]
85, trong đó các ký hiệu trích dẫ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__]]
86] 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 ngoặc đơ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__]]
86] và dấu ngoặc ké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__]]
88]. Bạn có thể đọc thêm về cách xác định chuỗi trong

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 mình và thực thi nó trực tiế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__]]
89] hoặc sử dụng phép thuậ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__]]
90 trong IPython hoặc Jupyter Notebooks

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

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

Việc thêm đối số

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__]]
91 sẽ chạy mã trong 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__]]
95 của gói. Bạn có thể tìm thêm thông tin về tệ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__]]
95 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__]]
7 có cùng giá trị. chuỗ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__]]
85

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

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ẽ 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__]]
85

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ủa mô-đun được đặt bằ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__]]
85 khi được đọc từ đầu vào tiêu 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__]]
7 được lưu trữ trong không gian tên chung của mô-đun cùng với các thuộc tí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__]]
34,
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__]]
35 và 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ụ thể cho các mô-đun và gói, trong

Loại bỏ các quảng cáo

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 xem cách thứ hai mà trình thông dịch Python sẽ thực thi mã của bạn. nhập khẩu. 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ể thực hiện với

Trong quá trình nhập, Python thực thi các câu lệnh được xác định trong mô-đun đã chỉ định [nhưng chỉ trong lần đầu tiên bạn nhập mô-đun]. Để minh họa kết quả nhập tệ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__]]
61 của bạn, hãy khởi động trình thông dịch Python tương tác rồi nhập tệ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__]]
61 của bạ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__]]
8

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 lệnh gọi tớ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__]]
62. Hai dòng đầu tiên hoàn toàn giống với 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 ở một trong hai dòng đầu tiên. Tuy nhiên, có một sự khác biệt trong đầu 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__]]
62 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 với tên của mô-đun đang được nhập. Bạn có thể thấy điều này trong dòng đầu ra thứ ba ở 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ị
eleanor@realpython:~/Documents$
23, là tên của tệ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__]]
71 mà Python đang nhập từ đó

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

Ghi chú. Để biết thêm thông tin về cách nhập hoạt động trong Python, hãy xem nhập Python. Các kỹ thuật và mẹo nâng cao cũng như nhập tuyệt đối và tương đối 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ó, sẽ rất hữu ích nếu bạn biết một số phương pháp hay nhất để sử dụng. Những điều này sẽ á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 vào mô-đun khác hoặc phiên tương tác

Bạn sẽ tìm hiểu về bốn phương pháp hay 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 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 hàm có tên 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 mã vào một chức năng hoặc lớp

Hãy nhớ rằng trình thông dịch Python thực thi tất cả mã trong 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 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 tin 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 nó nhập mô-đun của bạn

Do đó, cách tốt nhất là bao gồm hầu hết 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 , nó chỉ lưu trữ các định nghĩa đó để sử dụng sau này và không thực sự thực thi chúng cho đến khi bạn yêu cầu nó

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

eleanor@realpython:~/Documents$
81 để minh họa ý tưởng 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__]]
9

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

eleanor@realpython:~/Documents$
82 từ mô-đun
eleanor@realpython:~/Documents$
83

eleanor@realpython:~/Documents$
82 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 trong ví dụ này. Tiếp theo, bạ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__]]
62 để in một câu mô tả mục đích của mã này

Sau đó, bạn định nghĩa một hàm có tên là

eleanor@realpython:~/Documents$
86 thực hiện năm việc

  1. In một số đầu ra để cho 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 thực thi trong ba giây bằng cách sử dụng
    eleanor@realpython:~/Documents$
    
    82
  4. In một số đầu ra để cho người dùng biết rằng quá trình xử lý đã kết thúc
  5. Trả về dữ liệu đã sửa đổi

Thực thi 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

eleanor@realpython:~/Documents$
88 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__]]
62 nằm ngoài định nghĩa hàm, sau đó nó sẽ tạo ra định nghĩa của hàm có tên là
eleanor@realpython:~/Documents$
86. Sau đó, tập lệnh sẽ thoát 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
eleanor@realpython:~/Documents$
86

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

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

Đầu ra mà chúng ta có thể thấy ở đây là kết quả 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__]]
62 đầu tiên. Lưu ý rằng việc nhập từ
eleanor@realpython:~/Documents$
83 và xác định
eleanor@realpython:~/Documents$
86 không tạo ra kết quả nào. Cụ thể, đầu ra của các lệnh gọi tớ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__]]
62 bên trong định nghĩa của
eleanor@realpython:~/Documents$
86 không được in

Nhập tệp các phương pháp hay nhất vào 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 vào một phiên tương tác [hoặc một 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

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 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 đó gõ

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__]]
07

>>>

eleanor@realpython:~/Documents$
2

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

eleanor@realpython:~/Documents$
81 là từ lệnh gọ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__]]
62 đầu tiên được xác định bên ngoài
eleanor@realpython:~/Documents$
86. Nhập từ
eleanor@realpython:~/Documents$
83 và xác định
eleanor@realpython:~/Documents$
86 không tạo ra kết quả nào, giống như khi bạn thực thi mã từ dòng lệnh

Loại bỏ các quảng cáo

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__]]
13 để 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

eleanor@realpython:~/Documents$
86 thực thi khi bạn chạy tập lệnh từ dòng lệnh chứ 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__]]
13 để xác định ngữ cảnh thực thi và chỉ chạy có điều kiện
eleanor@realpython:~/Documents$
86 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
eleanor@realpython:~/Documents$
5. Thêm mã bên dưới vào cuối tệp
eleanor@realpython:~/Documents$
81 của bạn

eleanor@realpython:~/Documents$
8

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__]]
7. Điều kiện này sẽ đánh giá thành
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 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 tập lệnh đó

Bên trong khối đ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__]]
    
    26 lưu trữ dữ liệu bạn đã lấy từ Web và in nó
  • Dòng 14. Bạn đang xử lý dữ liệu
  • dòng 15. Bạn đang in dữ liệu đã sửa đổi

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

eleanor@realpython:~/Documents$
81 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 hiển thị kết quả của cuộc gọ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__]]
62 bên ngoài
eleanor@realpython:~/Documents$
86

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__]]
26 được in ra. Đ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, vì vậy câu lệnh điều kiện được đánh giá là
eleanor@realpython:~/Documents$
7

Tiếp theo, tập lệnh của bạn có tên là

eleanor@realpython:~/Documents$
86 và đã 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__]]
26 để sửa đổi. Khi
eleanor@realpython:~/Documents$
86 thực thi, nó sẽ 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__]]
37 được in ra

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

eleanor@realpython:~/Documents$
81 từ trình thông dịch tương tác [hoặc mô-đun khác]. Ví dụ dưới đây minh họa tình huống này

>>>

eleanor@realpython:~/Documents$
2

Lưu ý rằng bạn có hành vi tương tự 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 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ị
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__]]
40, vì vậy Python không thực thi mã bên trong khối, bao gồm cả
eleanor@realpython:~/Documents$
86, vì câu 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__]]
42

Tạo một hàm được 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ể 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 ý của bạn

Nhiều ngôn ngữ, chẳng hạn như C, C++, Java và một số ngôn ngữ khác, định nghĩa một chức năng đặ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 tự động gọi khi thực thi chương trình đã biên dịch. Chức năng này thường được gọi là điểm vào vì nó là nơi thực thi vào chương trình

Ngược lại, Python không có chức năng đặc biệt đóng vai trò là điểm vào của tập lệnh. Bạn thực sự có thể đặt cho hàm điểm vào 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 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, nhưng cách tốt nhất là đặt tên cho hàm điểm đầu vào 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. 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 đều biết ngay 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__]]
6 phải chứa bất kỳ mã nào mà 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 là đặt mã trực tiếp vào khối đ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

eleanor@realpython:~/Documents$
81 để 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__]]
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 nằm trong khối điều kiện. Sau đó, bạn đã thay đổi khối đ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 mã, bạn sẽ nhận được kết quả giống như trong phần trước

Loại bỏ các quảng cáo

Gọi các hàm khác từ hàm main[]

Một thực tế phổ biến khác trong Python 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 thực thi các chức năng khác, thay vì bao gồm mã hoàn thành tác 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 tác vụ tổng thể của mình từ một số tác vụ phụ nhỏ hơn có thể thực thi độc lập

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

  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 API web
  2. Xử lý dữ liệu
  3. Ghi dữ liệu đã xử lý vào một vị trí khác

Nếu bạn triển khai từng nhiệm vụ phụ 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 số bước và bỏ qua những bước bạn không muốn. Sau đó, bạn có thể tạo 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__]]
6 và bạn có thể tận hưởng cả hai thế giới tốt nhất

Việc có nên áp dụng phương pháp này vào mã của bạn hay không là quyết định từ phía bạn. 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 người khác đang cố 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 luồng chương trình

Sửa đổi tệp

eleanor@realpython:~/Documents$
81 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 đó. Đị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 đã 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 định nghĩa. 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 để nó lần lượt gọi các hàm đọ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__]]
26 đượ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__]]
58.
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__]]
26 này được chuyển đến
eleanor@realpython:~/Documents$
86, 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__]]
37. 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__]]
37 đượ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__]]
63

Hai dòng cuối cùng của tập 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__]]
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ộ quy trình xử lý từ dòng lệnh, như hình 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 kết quả từ lần thực thi 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 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__]]
58,
eleanor@realpython:~/Documents$
86 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__]]
63. Tuy nhiên, bạn cũng có thể nhập tệp
eleanor@realpython:~/Documents$
81 và sử dụng lại
eleanor@realpython:~/Documents$
86 cho một nguồn dữ liệu đầu vào khác, như minh họa 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__]]
74 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__]]
75 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

eleanor@realpython:~/Documents$
81, vì vậy đầ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__]]
26 thay vì đọc dữ liệu từ Web. Sau đó, bạn đã sử dụng lại
eleanor@realpython:~/Documents$
86 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__]]
63 từ tệp
eleanor@realpython:~/Documents$
81. Trong trường hợp này, bạn đã tận dụng lợi thế của việc 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__]]
6

Tóm tắt các phương pháp hay nhất về chức năng chính của 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__]]
6 trong Python mà bạn vừa xem

  1. Đặt mã mất nhiều thời gian để chạy hoặc có tác dụng khác trên máy tính trong một hàm hoặc lớp, để bạn có thể kiểm soát 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__]]
    
    7 để xác định ngữ cảnh và thay đổi hành vi 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 hàm đầu vào của mình 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 để truyền đạt mục đích của hàm, mặc dù Python không gán bất kỳ ý nghĩa đặc biệt nào cho hàm có tên 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

  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__]]
    
    6 và 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__]]
    
    6

Loại bỏ các quảng cáo

Phần kết luận

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

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

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

  • Việc 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 để viết 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 nhậ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__]]
    
    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__]]
      
      93 [để thực thi tệ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__]]
      
      95 của gói]
    • 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 phương pháp hay để 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ã hàm 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

Đánh dấu là đã hoàn thành

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Định nghĩa hàm chính trong Python

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Bryan Weber

Bryan là nhà phát triển cốt lõi của Cantera, nền tảng mã nguồn mở cho nhiệt động lực học, động học hóa học và vận chuyển. Là một nhà phát triển nói chung, Bryan thực hiện Python từ web đến khoa học dữ liệu và mọi nơi ở giữa

» Thông tin thêm về Bryan

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Brad

Joanna

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng các chuyên gia Pythonistas

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Chúng ta có thể chuyển đối số trong hàm chính trong Python không?

Chúng ta có thể truyền nhiều đối số cho một hàm python bằng cách xác định trước các tham số hình thức trong định nghĩa hàm .

Chúng ta có thể chuyển đối số trong hàm chính không?

Có, chúng ta có thể đưa ra đối số trong hàm main[] . Các đối số dòng lệnh trong C được chỉ định sau tên của chương trình trong dòng lệnh của hệ thống và các giá trị đối số này được chuyển đến chương trình của bạn trong quá trình thực thi chương trình. argc và argv là hai đối số có thể chuyển đến hàm chính.

Các đối số được truyền cho hàm chính là gì?

Hàm main[] có hai đối số mà theo truyền thống được gọi là argc và argv và trả về một số nguyên có dấu.

Chủ Đề