Hướng dẫn what is the default return value of a python function? - giá trị trả về mặc định của hàm python là gì?

Trong Python, có thể tạo ra một quy trình không có lợi nhuận rõ ràng. I E.:

def test(val):
    if 0 == val:
        return 8

Hơn nữa, có thể gán kết quả của chức năng đó cho một biến:

>>> a = test(7)
>>> print `a`
'None'

Tại sao cái quái đó là như vậy? Logic ngôn ngữ nào đằng sau quyết định thiết kế khó khăn đó? Tại sao điều đó không chỉ đơn giản là tăng một lỗi trình biên dịch?

Chỉnh sửa: Vâng, tôi nhận ra rằng nó hoạt động theo cách đó. Cảm ơn. Câu hỏi của tôi là tại sao? Có vẻ như một cách lửa chắc chắn như vậy để giới thiệu các lỗi tinh tế vào mã của bạn. Và dường như, như đã được đề cập dưới đây bởi E-Satis, để đi ngược lại câu ngạn ngữ Pythonic rất khôn ngoan rằng "rõ ràng là tốt hơn sau đó tiềm ẩn".

Vì vậy, có chuyện gì với điều này? Nó chỉ là một thiết kế giám sát hoặc một giả định đơn giản hóa hoặc một quyết định có chủ ý, được xem xét?

EDIT: Mọi người có đồng ý rằng việc thể hiện ý định này tốt hơn nhiều:

def test(val):
    if 0 == val:
        return 8
    else:
        return None

Nếu vậy, tại sao Python lại thích phong cách trước đây để tăng ngoại lệ thời gian biên dịch?

Chỉnh sửa: S.Lott đưa ra điểm rõ ràng (và những người khác cũng vậy, anh ta có vẻ rõ ràng nhất với tôi) rằng hành vi này là kết quả của việc Python là ngôn ngữ năng động và do đó không thể kiểm tra tại thời điểm biên dịch, Chạy hành vi thời gian của hệ thống.

Vì không có sự phân biệt giữa các thủ tục (các hàm đó không trả về giá trị) và các hàm (các hàm làm), nên không có gì để thực thi tại thời điểm biên dịch. Điều đó có nghĩa là khi chúng ta đến với hành vi thời gian chạy, chúng ta có một thất bại thảm khốc trong một số trường hợp (chúng ta ném một ngoại lệ) hoặc chúng ta âm thầm thất bại cho rằng lập trình viên biết họ đang làm gì và hiểu hành vi mặc định.

Cách Pythonic là làm theo cách sau, cách theo phong cách C là làm trước. Điều đó dường như có ý nghĩa như một lý do tốt để làm theo cách đó.
That seems to make sense as a good reason to do it that way.

Sự biện minh rõ ràng của S.Lott được chôn cất trong các ý kiến ​​cho câu trả lời được chấp nhận, vì vậy tôi nghĩ tốt nhất nên tóm tắt ở đây.

Tôi sẽ giữ cho việc chấp nhận câu trả lời một chút để xem liệu S.Lott có đưa ra câu trả lời chính thức không. Nếu không, tôi sẽ đưa ra các điểm cho Silentghost.

>>> a = test(7)
>>> print `a`
'None'
2
>>> a = test(7)
>>> print `a`
'None'
3
{'x': 20, 'str': 'GeeksforGeeks'}
8
>>> a = test(7)
>>> print `a`
'None'
83
>>> a = test(7)
>>> print `a`
'None'
76

Bản quyền 1999-2022 bởi dữ liệu refsnes. Đã đăng ký Bản quyền. W3Schools được cung cấp bởi W3.CSS.
W3Schools is Powered by W3.CSS.

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed.

    Bàn luận Return statement can not be used outside the function.

    Syntax:  

    def fun():
        statements
        .
        .
        return [expression]

    Example:

    def cube(x):
       r=x**3
       return r

    Example:

    Python3

    Một câu lệnh trả về được sử dụng để kết thúc việc thực hiện cuộc gọi chức năng và trả về kết quả (giá trị của biểu thức theo từ khóa trả về) cho người gọi. Các tuyên bố sau các tuyên bố trả lại không được thực thi. Nếu câu lệnh trả về không có bất kỳ biểu thức nào, thì giá trị đặc biệt không được trả về. & Nbsp; một bản trả về được sử dụng chung để gọi một hàm để có thể thực thi các câu lệnh được truyền.

    Lưu ý: Không thể sử dụng câu lệnh trả về bên ngoài chức năng.

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    1

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    >>> a = test(7)
    >>> print `a`
    'None'
    
    4
    >>> a = test(7)
    >>> print `a`
    'None'
    
    5
    >>> a = test(7)
    >>> print `a`
    'None'
    
    6

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    8

    def fun():
        statements
        .
        .
        return [expression]
    0
    def fun():
        statements
        .
        .
        return [expression]
    1
    def fun():
        statements
        .
        .
        return [expression]
    2
    def fun():
        statements
        .
        .
        return [expression]
    3
    def fun():
        statements
        .
        .
        return [expression]
    4
    def fun():
        statements
        .
        .
        return [expression]
    5

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    1
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    2

    def fun():
        statements
        .
        .
        return [expression]
    0
    def fun():
        statements
        .
        .
        return [expression]
    1
    def cube(x):
       r=x**3
       return r
    5
    def fun():
        statements
        .
        .
        return [expression]
    3
    def fun():
        statements
        .
        .
        return [expression]
    4
    def fun():
        statements
        .
        .
        return [expression]
    5

    Output:  

    Result of add function is 5
    
    Result of is_true function is True

    def test(val): if 0 == val: return 8 else: return None 3def test(val): if 0 == val: return 8 else: return None 4 def test(val): if 0 == val: return 8 else: return None 5def test(val): if 0 == val: return 8 else: return None 6def test(val): if 0 == val: return 8 else: return None 7def test(val): if 0 == val: return 8 else: return None 8def test(val): if 0 == val: return 8 else: return None 9

    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    3
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    def fun():
        statements
        .
        .
        return [expression]
    8
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    6
    def cube(x):
       r=x**3
       return r
    0
    def cube(x):
       r=x**3
       return r
    1
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    9

    • Trả về nhiều giá trị This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class. 

    Trong Python, chúng ta có thể trả về nhiều giá trị từ một hàm. Sau đây là những cách khác nhau. & NBSP; & nbsp;

    Python3

    Sử dụng đối tượng: Điều này tương tự như C/C ++ và Java, chúng ta có thể tạo một lớp (trong C, Struct) để giữ nhiều giá trị và trả về một đối tượng của lớp. & NBSP;

    Thí dụ

    def cube(x):
       r=x**3
       return r
    9
    Result of add function is 5
    
    Result of is_true function is True
    0

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    Result of add function is 5
    
    Result of is_true function is True
    3
    Result of add function is 5
    
    Result of is_true function is True
    4
    Result of add function is 5
    
    Result of is_true function is True
    5

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    def fun():
        statements
        .
        .
        return [expression]
    3
    Result of add function is 5
    
    Result of is_true function is True
    9
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    geeksforgeeks
    20
    1

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    geeksforgeeks
    20
    4
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4 ________ 66 & NBSP;

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    geeksforgeeks
    20
    8

    def fun():
        statements
        .
        .
        return [expression]
    0
    ['geeksforgeeks', 20]
    6
    Result of add function is 5
    
    Result of is_true function is True
    9
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    9

    def fun():
        statements
        .
        .
        return [expression]
    0
    {'x': 20, 'str': 'GeeksforGeeks'}
    0

    • >>> a = test(7)
      >>> print `a`
      'None'
      
      2
      >>> a = test(7)
      >>> print `a`
      'None'
      
      3
      ['geeksforgeeks', 20]
      1
      A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple.

    Python3

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    def fun():
        statements
        .
        .
        return [expression]
    3
    Result of add function is 5
    
    Result of is_true function is True
    9
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    geeksforgeeks
    20
    1

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    geeksforgeeks
    20
    4
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4 ________ 66 & NBSP;

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    geeksforgeeks
    20
    8

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    ['geeksforgeeks', 20]
    1

    ['geeksforgeeks', 20]
    2
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    ['geeksforgeeks', 20]
    4

    def fun():
        statements
        .
        .
        return [expression]
    0
    def fun():
        statements
        .
        .
        return [expression]
    1
    Result of add function is 5
    
    Result of is_true function is True
    9
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    9

    def fun():
        statements
        .
        .
        return [expression]
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    04

    • Output:  
    geeksforgeeks
    20
    • Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có (). Tuples là bất biến. Xem điều này để biết chi tiết về tuple. A list is like an array of items created using square brackets. They are different from arrays as they can contain items of different types. Lists are different from tuples as they are mutable. See this for details of list.

    Python3

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    def fun():
        statements
        .
        .
        return [expression]
    3
    Result of add function is 5
    
    Result of is_true function is True
    9
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    geeksforgeeks
    20
    1

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    geeksforgeeks
    20
    4
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4 ________ 66 & NBSP;

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    geeksforgeeks
    20
    8

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    ['geeksforgeeks', 20]
    1

    ['geeksforgeeks', 20]
    2
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    ['geeksforgeeks', 20]
    4

    def fun():
        statements
        .
        .
        return [expression]
    0
    def fun():
        statements
        .
        .
        return [expression]
    1
    >>> a = test(7)
    >>> print `a`
    'None'
    
    20
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    9

    • Output:  
    ['geeksforgeeks', 20]
    • Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có (). Tuples là bất biến. Xem điều này để biết chi tiết về tuple. A Dictionary is similar to hash or map in other languages. See this for details of dictionary.

    Python3

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    def fun():
        statements
        .
        .
        return [expression]
    3
    Result of add function is 5
    
    Result of is_true function is True
    9
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    geeksforgeeks
    20
    1

    Result of add function is 5
    
    Result of is_true function is True
    6
    Result of add function is 5
    
    Result of is_true function is True
    4
    geeksforgeeks
    20
    4
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4 ________ 66 & NBSP;

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    geeksforgeeks
    20
    8

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    ['geeksforgeeks', 20]
    1

    ['geeksforgeeks', 20]
    2
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    ['geeksforgeeks', 20]
    4

    Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có (). Tuples là bất biến. Xem điều này để biết chi tiết về tuple.

    def fun():
        statements
        .
        .
        return [expression]
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    53

    • Output:  
    {'x': 20, 'str': 'GeeksforGeeks'}

    >>> a = test(7) >>> print `a` 'None' 2Result of add function is 5 Result of is_true function is True9 def test(val): if 0 == val: return 8 else: return None 4 geeksforgeeks 201

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    {'x': 20, 'str': 'GeeksforGeeks'}
    8
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    geeksforgeeks
    20
    6

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    Result of add function is 5
    
    Result of is_true function is True
    9
    The result is 25
    
    The result is: 100
    4

    Python3

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    55

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    58

    Result of add function is 5
    
    Result of is_true function is True
    6
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    8
    >>> a = test(7)
    >>> print `a`
    'None'
    
    5
    >>> a = test(7)
    >>> print `a`
    'None'
    
    63

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    >>> a = test(7)
    >>> print `a`
    'None'
    
    66

    >>> a = test(7)
    >>> print `a`
    'None'
    
    67
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    >>> a = test(7)
    >>> print `a`
    'None'
    
    69
    >>> a = test(7)
    >>> print `a`
    'None'
    
    70
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    9

    def fun():
        statements
        .
        .
        return [expression]
    0
    def fun():
        statements
        .
        .
        return [expression]
    1
    >>> a = test(7)
    >>> print `a`
    'None'
    
    74
    >>> a = test(7)
    >>> print `a`
    'None'
    
    75
    >>> a = test(7)
    >>> print `a`
    'None'
    
    76
    >>> a = test(7)
    >>> print `a`
    'None'
    
    77

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    79

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    8
    >>> a = test(7)
    >>> print `a`
    'None'
    
    83
    >>> a = test(7)
    >>> print `a`
    'None'
    
    76

    >>> a = test(7)
    >>> print `a`
    'None'
    
    0
    >>> a = test(7)
    >>> print `a`
    'None'
    
    86

    >>> a = test(7)
    >>> print `a`
    'None'
    
    2
    >>> a = test(7)
    >>> print `a`
    'None'
    
    3
    >>> a = test(7)
    >>> print `a`
    'None'
    
    89

    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    3
    def test(val):
        if 0 == val:
            return 8
        else:
            return None
    
    4
    >>> a = test(7)
    >>> print `a`
    'None'
    
    92

    def fun():
        statements
        .
        .
        return [expression]
    0
    def fun():
        statements
        .
        .
        return [expression]
    1
    >>> a = test(7)
    >>> print `a`
    'None'
    
    95
    >>> a = test(7)
    >>> print `a`
    'None'
    
    96
    >>> a = test(7)
    >>> print `a`
    'None'
    
    76
    >>> a = test(7)
    >>> print `a`
    'None'
    
    77

    Output:  

    The result is 25
    
    The result is: 100

    Giá trị trả về mặc định cho một hàm là gì?

    Giá trị trả về mặc định từ một hàm là int. Trừ khi được chỉ định rõ ràng, giá trị trả về mặc định của trình biên dịch sẽ là giá trị số nguyên từ hàm.integer value from function.

    Return () làm gì trong Python?

    return () Trong python câu lệnh return (), như trong các ngôn ngữ lập trình khác kết thúc cuộc gọi hàm và trả về kết quả cho người gọi.Nó là một thành phần chính trong bất kỳ chức năng hoặc phương thức nào trong mã bao gồm từ khóa trả về và giá trị sẽ được trả về sau đó.ends the function call and returns the result to the caller. It is a key component in any function or method in a code which includes the return keyword and the value that is to be returned after that.

    Giá trị trả về của loại trong Python là gì?

    loại () hàm trong python.Phương thức loại () Trả về loại lớp của đối số (đối tượng) được truyền dưới dạng tham số trong Python.class type of the argument(object) passed as parameter in Python.

    Loại trả về ID hàm trong Python là gì?

    Hàm python id () trả về một danh tính của một đối tượng.Đây là một số nguyên được đảm bảo là duy nhất.Hàm này lấy một đối số một đối tượng và trả về một số nguyên duy nhất đại diện cho danh tính.Hai đối tượng có tuổi thọ không chồng chéo có thể có cùng giá trị id ().an identity of an object. This is an integer which is guaranteed to be unique. This function takes an argument an object and returns a unique integer number which represents identity. Two objects with non-overlapping lifetimes may have the same id() value.