Hướng dẫn what value do functions that do not contain return statement return in python? - giá trị nào làm các hàm không chứa câu lệnh trả về trả về trong python?

Đưa ra chức năng ví dụ này:

def writeFile(listLine,fileName):
    '''put a list of str-line into a file named fileName'''
    with open(fileName,'a',encoding = 'utf-8') as f:
        for line in listLine:
            f.writelines(line+'\r\n')
    return True

Tuyên bố này có làm bất cứ điều gì hữu ích không?

Sự khác biệt giữa nó và không có nó là gì? Điều gì sẽ xảy ra nếu không có chức năng trả lại?

Hướng dẫn what value do functions that do not contain return statement return in python? - giá trị nào làm các hàm không chứa câu lệnh trả về trả về trong python?

Karl Knechtel

59.4K10 Huy hiệu vàng86 Huy hiệu bạc132 Huy hiệu đồng10 gold badges86 silver badges132 bronze badges

Đã hỏi ngày 12 tháng 3 năm 2013 lúc 6:52Mar 12, 2013 at 6:52

Hướng dẫn what value do functions that do not contain return statement return in python? - giá trị nào làm các hàm không chứa câu lệnh trả về trả về trong python?

3

Nếu một hàm không chỉ định giá trị trả về, nó sẽ trả về None.

Trong một câu lệnh IF/SEN có điều kiện, None đánh giá là sai. Vì vậy, về lý thuyết, bạn có thể kiểm tra giá trị trả về của hàm này để thành công/thất bại. Tôi nói "trong lý thuyết" bởi vì đối với mã trong câu hỏi này, chức năng không bắt hoặc xử lý các ngoại lệ và có thể yêu cầu làm cứng thêm.

Đã trả lời ngày 12 tháng 3 năm 2013 lúc 6:57Mar 12, 2013 at 6:57

Travis Beartravis gấuTravis Bear

12.3k6 Huy hiệu vàng40 Huy hiệu bạc49 Huy hiệu đồng6 gold badges40 silver badges49 bronze badges

2

Hàm luôn trả về None nếu rõ ràng

def fun():
    statements
    .
    .
    return [expression]
2 không được viết.

Đã trả lời ngày 12 tháng 3 năm 2013 lúc 7:00Mar 12, 2013 at 7:00

Hướng dẫn what value do functions that do not contain return statement return in python? - giá trị nào làm các hàm không chứa câu lệnh trả về trả về trong python?

GodmangodmanGodMan

2.5292 Huy hiệu vàng23 Huy hiệu bạc39 Huy hiệu Đồng2 gold badges23 silver badges39 bronze badges

Nếu bạn có return True ở cuối chức năng, bạn có thể nói những thứ như:

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

Tuy nhiên, bởi vì nó sẽ luôn luôn là

def fun():
    statements
    .
    .
    return [expression]
5, nó hoàn toàn vô nghĩa. Sẽ tốt hơn nếu trả về đúng nếu tệp được viết chính xác, v.v.

Nếu bạn không trả lại rõ ràng bất cứ điều gì, giá trị sẽ là None

Đã trả lời ngày 12 tháng 3 năm 2013 lúc 7:04Mar 12, 2013 at 7:04

Will Richardswill RichardsonWill Richardson

7.5626 Huy hiệu vàng43 Huy hiệu bạc56 Huy hiệu Đồng6 gold badges43 silver badges56 bronze badges

1

Không có nhiều ý nghĩa khi tự mình có một tuyên bố trả lại mà không được quy kết hoặc kiểm tra chức năng.

Python trả lại không nếu không có gì được trả lại. Trong trường hợp của bạn, có lẽ bạn nên trả về đúng nếu tệp mở và ghi thành công

Đã trả lời ngày 12 tháng 3 năm 2013 lúc 7:11Mar 12, 2013 at 7:11

Dora Doradora

1.1942 Huy hiệu vàng20 Huy hiệu bạc38 Huy hiệu Đồng2 gold badges20 silver badges38 bronze badges

1

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.

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

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    def cube(x):
       r=x**3
       return r
    1
    def cube(x):
       r=x**3
       return r
    2
    def cube(x):
       r=x**3
       return r
    3

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

    Result of add function is 5
    
    Result of is_true function is True
    7
    Result of add function is 5
    
    Result of is_true function is True
    8
    Result of add function is 5
    
    Result of is_true function is True
    9
    geeksforgeeks
    20
    0
    geeksforgeeks
    20
    1
    geeksforgeeks
    20
    2

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    def cube(x):
       r=x**3
       return r
    8
    def cube(x):
       r=x**3
       return r
    9

    Result of add function is 5
    
    Result of is_true function is True
    7
    Result of add function is 5
    
    Result of is_true function is True
    8
    ['geeksforgeeks', 20]
    2
    geeksforgeeks
    20
    0
    geeksforgeeks
    20
    1
    geeksforgeeks
    20
    2

    Output:  

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

    Result of add function is 5 Result of is_true function is True0Result of add function is 5 Result of is_true function is True1 Result of add function is 5 Result of is_true function is True2Result of add function is 5 Result of is_true function is True3Result of add function is 5 Result of is_true function is True4Result of add function is 5 Result of is_true function is True5Result of add function is 5 Result of is_true function is True6

    Result of add function is 5
    
    Result of is_true function is True
    0
    Result of add function is 5
    
    Result of is_true function is True
    1
    geeksforgeeks
    20
    5
    Result of add function is 5
    
    Result of is_true function is True
    3
    geeksforgeeks
    20
    7
    geeksforgeeks
    20
    8
    Result of add function is 5
    
    Result of is_true function is True
    6

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

    ['geeksforgeeks', 20]
    6
    ['geeksforgeeks', 20]
    7

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

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    geeksforgeeks
    20
    0
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    Result of add function is 5
    
    Result of is_true function is True
    1
    {'x': 20, 'str': 'GeeksforGeeks'}
    8

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    The result is 25
    
    The result is: 100
    1
    Result of add function is 5
    
    Result of is_true function is True
    1 ________ 73 & nbsp; & nbsp;

    def fun():
        statements
        .
        .
        return [expression]
    7
    The result is 25
    
    The result is: 100
    5

    Result of add function is 5
    
    Result of is_true function is True
    7return True3
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    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
    7return True7

    • def fun():
          statements
          .
          .
          return [expression]
      9
      def fun():
          statements
          .
          .
          return [expression]
      2
      The result is 25
      
      The result is: 100
      8
      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

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    geeksforgeeks
    20
    0
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    Result of add function is 5
    
    Result of is_true function is True
    1
    {'x': 20, 'str': 'GeeksforGeeks'}
    8

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    The result is 25
    
    The result is: 100
    1
    Result of add function is 5
    
    Result of is_true function is True
    1 ________ 73 & nbsp; & nbsp;

    def fun():
        statements
        .
        .
        return [expression]
    7
    The result is 25
    
    The result is: 100
    5

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    The result is 25
    
    The result is: 100
    8

    The result is 25
    
    The result is: 100
    9
    Result of add function is 5
    
    Result of is_true function is True
    1 return True1

    Result of add function is 5
    
    Result of is_true function is True
    7
    Result of add function is 5
    
    Result of is_true function is True
    8
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    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
    7
    def fun():
        statements
        .
        .
        return [expression]
    11

    • 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

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    geeksforgeeks
    20
    0
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    Result of add function is 5
    
    Result of is_true function is True
    1
    {'x': 20, 'str': 'GeeksforGeeks'}
    8

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    The result is 25
    
    The result is: 100
    1
    Result of add function is 5
    
    Result of is_true function is True
    1 ________ 73 & nbsp; & nbsp;

    def fun():
        statements
        .
        .
        return [expression]
    7
    The result is 25
    
    The result is: 100
    5

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    The result is 25
    
    The result is: 100
    8

    The result is 25
    
    The result is: 100
    9
    Result of add function is 5
    
    Result of is_true function is True
    1 return True1

    Result of add function is 5
    
    Result of is_true function is True
    7
    Result of add function is 5
    
    Result of is_true function is True
    8
    def fun():
        statements
        .
        .
        return [expression]
    27
    Result of add function is 5
    
    Result of is_true function is True
    6

    • 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

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    geeksforgeeks
    20
    0
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    Result of add function is 5
    
    Result of is_true function is True
    1
    {'x': 20, 'str': 'GeeksforGeeks'}
    8

    {'x': 20, 'str': 'GeeksforGeeks'}
    3
    {'x': 20, 'str': 'GeeksforGeeks'}
    1
    The result is 25
    
    The result is: 100
    1
    Result of add function is 5
    
    Result of is_true function is True
    1 ________ 73 & nbsp; & nbsp;

    def fun():
        statements
        .
        .
        return [expression]
    7
    The result is 25
    
    The result is: 100
    5

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    The result is 25
    
    The result is: 100
    8

    The result is 25
    
    The result is: 100
    9
    Result of add function is 5
    
    Result of is_true function is True
    1 return True1

    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.

    Result of add function is 5
    
    Result of is_true function is True
    7
    def fun():
        statements
        .
        .
        return [expression]
    60

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

    def fun(): statements . . return [expression]9{'x': 20, 'str': 'GeeksforGeeks'}6 Result of add function is 5 Result of is_true function is True1 {'x': 20, 'str': 'GeeksforGeeks'}8

    def fun():
        statements
        .
        .
        return [expression]
    9None5
    Result of add function is 5
    
    Result of is_true function is True
    1
    The result is 25
    
    The result is: 100
    3

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    def fun():
        statements
        .
        .
        return [expression]
    01

    Python3

    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    def fun():
        statements
        .
        .
        return [expression]
    03
    Result of add function is 5
    
    Result of is_true function is True
    1
    def fun():
        statements
        .
        .
        return [expression]
    05

    Sử dụng danh sách: Danh sách giống như một mảng các mục được tạo bằng dấu ngoặc vuông. Chúng khác với các mảng vì chúng có thể chứa các vật phẩm của các loại khác nhau. Danh sách khác với các bộ dữ liệu vì chúng có thể thay đổi. Xem điều này để biết chi tiết về danh sách.

    def fun():
        statements
        .
        .
        return [expression]
    9None5
    Result of add function is 5
    
    Result of is_true function is True
    1 ________ 73 & nbsp; & nbsp;

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    def fun():
        statements
        .
        .
        return [expression]
    24
    {'x': 20, 'str': 'GeeksforGeeks'}
    6
    def fun():
        statements
        .
        .
        return [expression]
    26

    def fun():
        statements
        .
        .
        return [expression]
    27
    Result of add function is 5
    
    Result of is_true function is True
    1 return True1

    Result of add function is 5
    
    Result of is_true function is True
    7
    Result of add function is 5
    
    Result of is_true function is True
    8
    def fun():
        statements
        .
        .
        return [expression]
    81
    def fun():
        statements
        .
        .
        return [expression]
    82
    def fun():
        statements
        .
        .
        return [expression]
    83
    def fun():
        statements
        .
        .
        return [expression]
    84

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

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2 None5
    def fun():
        statements
        .
        .
        return [expression]
    90
    def fun():
        statements
        .
        .
        return [expression]
    83

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

    def fun():
        statements
        .
        .
        return [expression]
    9
    def fun():
        statements
        .
        .
        return [expression]
    2
    def fun():
        statements
        .
        .
        return [expression]
    96

    Result of add function is 5
    
    Result of is_true function is True
    0
    Result of add function is 5
    
    Result of is_true function is True
    1
    def fun():
        statements
        .
        .
        return [expression]
    99

    Result of add function is 5
    
    Result of is_true function is True
    7
    Result of add function is 5
    
    Result of is_true function is True
    8
    def cube(x):
       r=x**3
       return r
    02
    def cube(x):
       r=x**3
       return r
    03
    def fun():
        statements
        .
        .
        return [expression]
    83
    def fun():
        statements
        .
        .
        return [expression]
    84

    Output:  

    The result is 25
    
    The result is: 100

    Hàm không trả về bất kỳ giá trị nào trong Python là gì?

    Giải thích: Python xác định rõ ràng đối tượng không được trả về nếu không có giá trị nào được chỉ định.None object that is returned if no value is specified.

    Hàm nào không có giá trị trả về?

    Các hàm vô hiệu (không trả lại giá trị): Các hàm void được tạo và sử dụng giống như các hàm trả lại giá trị ngoại trừ chúng không trả về một giá trị sau khi hàm thực thi.: Void functions are created and used just like value-returning functions except they do not return a value after the function executes.