Hướng dẫn return multiple values from a function python - trả về nhiều giá trị từ một hàm python

Bài viết này mô tả cách trả về nhiều giá trị từ một hàm trong Python.

  • Trả về nhiều giá trị bằng dấu phẩy
  • Trả lại
    result = test()
    
    print(result)
    print(type(result))
    # ('abc', 100)
    # 
    
    2

Xem bài viết sau đây cho những điều cơ bản của các chức năng trong Python.

  • Xác định và gọi các chức năng trong Python (def, return)

Trả về nhiều giá trị bằng dấu phẩy

Trả lại

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
2

Xem bài viết sau đây cho những điều cơ bản của các chức năng trong Python.

def test():
    return 'abc', 100

Xác định và gọi các chức năng trong Python (def, return)

Trong Python, bạn có thể trả về nhiều giá trị bằng cách đơn giản là chúng được phân tách bằng dấu phẩy.
Built-in Types - Tuples — Python 3.7.4 documentation

  • Ví dụ, xác định một hàm trả về một chuỗi và số nguyên như sau:

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 

Trong Python, các giá trị phân tách bằng dấu phẩy được coi là các bộ đếm không có dấu ngoặc đơn, ngoại trừ khi được yêu cầu bởi cú pháp. Vì lý do này, hàm trong ví dụ trên trả về một tuple với mỗi giá trị dưới dạng một phần tử.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 

Lưu ý rằng đó thực sự là dấu phẩy tạo ra một tuple, không phải dấu ngoặc đơn. Các dấu ngoặc đơn là tùy chọn, ngoại trừ trong trường hợp tuple trống hoặc khi chúng cần thiết để tránh sự mơ hồ của cú pháp. Các loại tích hợp - Tuples - Python 3.7.4 Tài liệu

# print(result[2])
# IndexError: tuple index out of range

Một tuple với một yếu tố yêu cầu một dấu phẩy trong Python

  • Mỗi phần tử có một loại được xác định trong hàm.

a, b = test()

print(a)
# abc

print(b)
# 100

Tất nhiên, việc chỉ định một chỉ mục vượt quá số lượng giá trị trả về được xác định làm tăng lỗi.

def test2():
    return 'abc', 100, [0, 1, 2]

a, b, c = test2()

print(a)
# abc

print(b)
# 100

print(c)
# [0, 1, 2]

Trả lại result = test() print(result) print(type(result)) # ('abc', 100) # 2

Xem bài viết sau đây cho những điều cơ bản của các chức năng trong Python.

def test_list():
    return ['abc', 100]

result = test_list()

print(result)
print(type(result))
# ['abc', 100]
# 

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

1) 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 (tính bằng C, Struct) để giữ nhiều giá trị và trả về một đối tượng của lớp. Using Object: 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.

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
8
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
9

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0____21
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
2
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
3
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
4

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
5
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
3
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
7
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
# print(result[2])
# IndexError: tuple index out of range
0

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
5
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
3
# print(result[2])
# IndexError: tuple index out of range
3
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9 ________ 35 & nbsp; & nbsp; & nbsp;

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
1
# print(result[2])
# IndexError: tuple index out of range
7

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
3
a, b = test()

print(a)
# abc

print(b)
# 100
0

a, b = test()

print(a)
# abc

print(b)
# 100
1
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
a, b = test()

print(a)
# abc

print(b)
# 100
3

a, b = test()

print(a)
# abc

print(b)
# 100
4
a, b = test()

print(a)
# abc

print(b)
# 100
5
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
a, b = test()

print(a)
# abc

print(b)
# 100
7

a, b = test()

print(a)
# abc

print(b)
# 100
4
a, b = test()

print(a)
# abc

print(b)
# 100
9

Output:

geeksforgeeks
20

Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.

2) 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 và Danh sách. A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple and list.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
1
# print(result[2])
# IndexError: tuple index out of range
7

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
3
a, b = test()

print(a)
# abc

print(b)
# 100
0

a, b = test()

print(a)
# abc

print(b)
# 100
1
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
a, b = test()

print(a)
# abc

print(b)
# 100
3

Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.

2) 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 và Danh sách.

a, b = test()

print(a)
# abc

print(b)
# 100
4
def test_list():
    return ['abc', 100]

result = test_list()

print(result)
print(type(result))
# ['abc', 100]
# 
9
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
a, b = test()

print(a)
# abc

print(b)
# 100
7

a, b = test()

print(a)
# abc

print(b)
# 100
4
geeksforgeeks
20
3

Output:

geeksforgeeks
20

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
# print(result[2])
# IndexError: tuple index out of range
0
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.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
1
# print(result[2])
# IndexError: tuple index out of range
7

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
3
a, b = test()

print(a)
# abc

print(b)
# 100
0

a, b = test()

print(a)
# abc

print(b)
# 100
1
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
a, b = test()

print(a)
# abc

print(b)
# 100
3

Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.

2) 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 và Danh sách.

a, b = test()

print(a)
# abc

print(b)
# 100
4
def test_list():
    return ['abc', 100]

result = test_list()

print(result)
print(type(result))
# ['abc', 100]
# 
9
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
2
a, b = test()

print(a)
# abc

print(b)
# 100
7

Output:

['geeksforgeeks', 20]

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
# print(result[2])
# IndexError: tuple index out of range
0
A Dictionary is similar to hash or map in other languages. See this for details of dictionary.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
1
# print(result[2])
# IndexError: tuple index out of range
7

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
3
a, b = test()

print(a)
# abc

print(b)
# 100
0

a, b = test()

print(a)
# abc

print(b)
# 100
1
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
a, b = test()

print(a)
# abc

print(b)
# 100
3

Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.

2) 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 và Danh sách.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
# print(result[2])
# IndexError: tuple index out of range
0

a, b = test()

print(a)
# abc

print(b)
# 100
4
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
22

Output:

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
0

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
def test2():
    return 'abc', 100, [0, 1, 2]

a, b, c = test2()

print(a)
# abc

print(b)
# 100

print(c)
# [0, 1, 2]
7
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
# print(result[2])
# IndexError: tuple index out of range
5
5) Using Data Class (Python 3.7+): In Python 3.7 and above the Data Class can be used to return a class with automatically added unique methods. The Data Class module has a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() in the user-defined classes.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
3
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
def test_list():
    return ['abc', 100]

result = test_list()

print(result)
print(type(result))
# ['abc', 100]
# 
3

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
27

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
def test_list():
    return ['abc', 100]

result = test_list()

print(result)
print(type(result))
# ['abc', 100]
# 
5
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
def test_list():
    return ['abc', 100]

result = test_list()

print(result)
print(type(result))
# ['abc', 100]
# 
7

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
31
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
34
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
35

3) 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.

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
geeksforgeeks
20
1
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9 ________ 35 & nbsp; & nbsp; & nbsp;

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
3
geeksforgeeks
20
6
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
8
geeksforgeeks
20
8

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
2
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
a, b = test()

print(a)
# abc

print(b)
# 100
3

4) Sử dụng từ điển: Từ điển tương tự như băm hoặc bản đồ bằng các ngôn ngữ khác. Xem điều này để biết chi tiết về từ điển.

a, b = test()

print(a)
# abc

print(b)
# 100
4
geeksforgeeks
20
3

a, b = test()

print(a)
# abc

print(b)
# 100
4
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
72

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
73
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
75
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
76

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
77
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
78
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
80
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
76

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
77
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
83
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
64
a, b = test()

print(a)
# abc

print(b)
# 100
7

Output:

result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
1

Reference:http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python
http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python

print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
0
['geeksforgeeks', 20]
9
print(result[0])
print(type(result[0]))
# abc
# 

print(result[1])
print(type(result[1]))
# 100
# 
9
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
01
result = test()

print(result)
print(type(result))
# ('abc', 100)
# 
02Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.


Làm cách nào để trả về nhiều giá trị từ một hàm?

Nếu chúng ta muốn hàm trả về nhiều giá trị của cùng loại dữ liệu, chúng ta có thể trả lại con trỏ về mảng của các loại dữ liệu đó.Chúng ta cũng có thể làm cho hàm trả về nhiều giá trị bằng cách sử dụng các đối số của hàm.return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.

Làm cách nào để trả về nhiều tham số trong Python?

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