Hướng dẫn should you return none python? - bạn không nên trả lại con trăn nào?

Về hành vi thực tế, không có sự khác biệt. Tất cả họ trở lại None và đó là nó. Tuy nhiên, có một thời gian và địa điểm cho tất cả những điều này. Các hướng dẫn sau đây về cơ bản là cách sử dụng các phương pháp khác nhau (hoặc ít nhất là cách tôi được dạy chúng nên được sử dụng), nhưng chúng không phải là quy tắc tuyệt đối để bạn có thể trộn chúng nếu bạn cảm thấy cần thiết.

Sử dụng return None

Điều này cho biết rằng hàm thực sự có nghĩa là trả về một giá trị cho việc sử dụng sau này và trong trường hợp này, nó trả về None. Giá trị này None sau đó có thể được sử dụng ở nơi khác. return None không bao giờ được sử dụng nếu không có giá trị trả về khả thi nào khác từ hàm.

Trong ví dụ sau, chúng tôi trở lại ____ 8 của mother nếu person được đưa ra là một con người. Nếu đó không phải là con người, chúng ta sẽ trở lại Noneperson không có mother (giả sử đó không phải là động vật hay gì đó).

def get_mother(person):
    if is_human(person):
        return person.mother
    else:
        return None

Sử dụng def find_prisoner_with_knife(prisoners): for prisoner in prisoners: if "knife" in prisoner.items: prisoner.move_to_inquisition() return # no need to check rest of the prisoners nor raise an alert raise_alert() 4

Điều này được sử dụng vì lý do tương tự như

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
5 trong các vòng lặp. Giá trị trả về không quan trọng và bạn chỉ muốn thoát toàn bộ hàm. Nó cực kỳ hữu ích ở một số nơi, mặc dù bạn không cần nó thường xuyên.

Chúng tôi đã có 15

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
6 và chúng tôi biết một trong số họ có một con dao. Chúng tôi lặp qua từng
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
7 từng người một để kiểm tra xem họ có dao không. Nếu chúng ta đánh người bằng dao, chúng ta có thể thoát khỏi chức năng vì chúng ta biết chỉ có một con dao và không có lý do gì khi kiểm tra phần còn lại của
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
6. Nếu chúng ta không tìm thấy
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
7 bằng một con dao, chúng ta sẽ đưa ra một cảnh báo. Điều này có thể được thực hiện theo nhiều cách khác nhau và sử dụng
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 có lẽ thậm chí không phải là cách tốt nhất, nhưng đó chỉ là một ví dụ để chỉ ra cách sử dụng
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 để thoát khỏi chức năng.

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()

Lưu ý: Bạn không bao giờ nên làm

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother
2, vì giá trị trả về không có nghĩa là bị bắt.

Không sử dụng def find_prisoner_with_knife(prisoners): for prisoner in prisoners: if "knife" in prisoner.items: prisoner.move_to_inquisition() return # no need to check rest of the prisoners nor raise an alert raise_alert() 4 ở tất cả

Điều này cũng sẽ trả lại None, nhưng giá trị đó không có nghĩa là được sử dụng hoặc bắt được. Nó đơn giản có nghĩa là chức năng kết thúc thành công. Về cơ bản, nó giống như

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 trong các chức năng
def set_mother(person, mother):
    if is_human(person):
        person.mother = mother
6 trong các ngôn ngữ như C ++ hoặc Java.

Trong ví dụ sau, chúng tôi đặt tên của mẹ và sau đó chức năng thoát ra sau khi hoàn thành thành công.

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother

Lưu ý: Bạn không bao giờ nên làm

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother
7, vì giá trị trả về không có nghĩa là bị bắt.

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 và một cộng đồng chuyên gia Pythonistas.

Mở khóa bài học này

Bài học này chỉ dành cho các thành viên. 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 và một cộng đồng chuyên gia Pythonistas.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Mở khóa bài học này

Xin lỗi! Có vẻ như có một vấn đề với phát lại video 🙁 Điều này có thể là do sự cố ngừng hoạt động tạm thời hoặc do sự cố cấu hình với trình duyệt của bạn. Vui lòng xem Hướng dẫn khắc phục sự cố trình phát video của chúng tôi để giải quyết vấn đề. Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Hướng dẫn should you return none python? - bạn không nên trả lại con trăn nào?

  • Bảng điểm
  • Nhận xét & Thảo luận

00:00 Bây giờ bạn đã biết cú pháp viết

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 trong Python, hãy để Lôi xem xét một số thực tiễn tốt nhất khi sử dụng nó. Đầu tiên, chúng tôi sẽ xem xét lại None một cách rõ ràng. Như bạn đã thấy, Python sẽ trả về giá trị None trong một số tình huống, nhưng có thể có những lúc thực sự viết ra một tuyên bố return None. Now that you know the syntax of writing
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statements in Python, let’s take a look at some best practices when using it. First, we’ll look at returning None explicitly. As you’ve seen, Python will return a value of None in several situations, but there might be occasions to actually write out a return None statement.

00:19 Về cơ bản có ba cách để gây ra giá trị None được trả về từ hàm: Nếu hàm không có câu lệnh

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4, nếu bạn có câu lệnh
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 mà không có giá trị trả về hoặc bạn có thể trả về rõ ràng None.
There are basically three ways to cause a value of None to be returned from a function: if the function doesn’t have a
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement at all, if you have a
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement with no return value, or you can explicitly return None.

00:35 Hãy để xác minh rằng cả ba trường hợp này thực sự trả về giá trị None. Vì vậy, ở đây, một chức năng không có tuyên bố

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4. Let’s verify that all three of these cases actually do return a value of None. So, here’s a function with no
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement.

00:47 Hãy nhớ rằng, chúng tôi sử dụng từ khóa None8 để chỉ ra một cơ thể hàm trống. Nếu chúng ta chỉ gọi chức năng này, Remember, we use the keyword None8 to indicate an empty function body. If we just call this function,

00:57 Chúng tôi không thấy bất cứ điều gì. Hãy nhớ rằng, người phiên dịch đã giành được giá trị trả lại là None. Bạn thực sự phải in kết quả của một cuộc gọi chức năng để thấy rằng nó thực sự trả về None. we don’t see anything. Remember, the interpreter won’t show a return value of None. You actually have to print the result of a function call to see that it does indeed return None.

01:11 Tiếp theo, chúng tôi sẽ xem xét chỉ có một tuyên bố

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 trần, Next, we’ll look at having just a bare
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement,

01:17 Vì vậy, chúng tôi sẽ có

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 nhưng không có giá trị trả lại. Nếu chúng ta in kết quả của cuộc gọi chức năng của nó, chúng ta sẽ nhận được None. Và cuối cùng, chúng ta có thể trả lại một cách rõ ràng None. so we’ll have
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 but no return value. If we print the result of its function call, we get None. And lastly, we can explicitly return None.

01:36 Chúng tôi trả lại None và in kết quả của cuộc gọi chức năng của nó. We return None, and print the result of its function call.

01:46 Chúng ta thấy chúng ta nhận được None một lần nữa. Vì vậy, tất cả những điều này trở lại None. Vì vậy, một số yếu tố cần xem xét khi bạn cần quyết định sử dụng nào? We see we get None again. So all of these return None. So, what are some factors to consider when you need to decide which to use?

01:56 Nếu chức năng của bạn thực hiện các hành động nhưng không có giá trị trả về rõ ràng và hữu ích, thì bạn có thể bỏ qua câu lệnh

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4, mặc dù bạn có thể sử dụng câu lệnh
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 trần chỉ để rõ ràng ý định của mình là trả lại từ chức năng.
If your function performs actions but doesn’t have a clear and useful return value, then you can omit the
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement, although you could use a bare
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement just to make clear your intention of returning from the function.

02:09 Nếu chức năng của bạn có nhiều nhánh với các câu lệnh

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 và trả lại None sẽ là một giá trị hợp lý cho một hoặc nhiều chi nhánh đó, thì bạn nên xem xét việc sử dụng rõ ràng return None thay vì dựa vào hành vi mặc định của Python. Một lời cảnh báo: Nhiều lập trình viên Python đến từ các ngôn ngữ lập trình khác, vì vậy hoàn toàn không có câu lệnh
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 cho một hàm có thể gây nhầm lẫn cho những người được sử dụng để đề cập đến các chương trình con như các thủ tục.
If your function has multiple branches with
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statements and returning None would be a reasonable value for one or more of those branches, then you should consider the explicit use of return None instead of relying on Python’s default behavior. A word of caution: many Python programmers come from other programming languages, so not having a
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
4 statement at all for a function might be confusing for those who are used to referring to such subprograms as procedures.

02:38 Đây có thể là một vấn đề cần xem xét khi suy nghĩ về khả năng duy trì lâu dài của dự án của bạn. Tiếp theo, chúng tôi sẽ xem xét hai thực tiễn tốt nhất khác liên quan đến việc ghi nhớ giá trị trả lại và tránh các biểu thức phức tạp. This might be an issue to consider in thinking about the long-term maintainability of your project. Next, we’ll look at two other best practices involving remembering the return value and avoiding complex expressions.

Bạn không nên trả lại trong Python?

Thông thường, khi đọc mã Python, bạn sẽ bắt gặp các chức năng tiện ích sẽ không trả lại.Mặc dù có vẻ tự nhiên khi không trả lại trong một số trường hợp, nhưng nên tránh vì nó có thể dễ bị lỗi.it should be avoided because it can be error-prone.

Điều gì xảy ra khi bạn trở lại không có Python?

Sau khi hoàn thành cuộc gọi chức năng, đầu ra các hàm được lưu trữ trong x.Nếu một hàm trả về không, không có gì để lưu trữ sau khi chức năng được gọi.there's nothing to store after the function is called.

Không có giá trị trả về?

Không có loại chỉ có một giá trị, không có.Không có gì cũng được sử dụng làm loại trả về cho các chức năng không trả về giá trị, tức là các hàm ngầm không trả về không.None is also used as the return type for functions that don't return a value, i.e. functions that implicitly return None .