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'
76Bả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ả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'
8def 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
1def test[val]:
if 0 == val:
return 8
else:
return None
2def fun[]: statements . . return [expression]0
def fun[]: statements . . return [expression]1
def cube[x]: r=x**3 return r5
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
def test[val]:
if 0 == val:
return 8
else:
return None
def test[val]:
if 0 == val:
return 8
else:
return None
def test[val]:
if 0 == val:
return 8
else:
return None
def test[val]:
if 0 == val:
return 8
else:
return None
def test[val]:
if 0 == val:
return 8
else:
return None
def test[val]:
if 0 == val:
return 8
else:
return None
def test[val]:
if 0 == val:
return 8
else:
return None
3def 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
6def cube[x]: r=x**3 return r0
def cube[x]: r=x**3 return r1
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 r9
Result of add function is 5 Result of is_true function is True0
>>> 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 True3
Result of add function is 5 Result of is_true function is True4
Result of add function is 5 Result of is_true function is True5
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True4
def fun[]: statements . . return [expression]3
Result 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
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True4
geeksforgeeks 204
def test[val]:
if 0 == val:
return 8
else:
return None
4 ________ 66 & NBSP;>>> a = test[7]
>>> print `a`
'None'
0 geeksforgeeks 208
def fun[]: statements . . return [expression]0
['geeksforgeeks', 20]6
Result of add function is 5 Result of is_true function is True9
def test[val]:
if 0 == val:
return 8
else:
return None
9def fun[]: statements . . return [expression]0
{'x': 20, 'str': 'GeeksforGeeks'}0
2>>> a = test[7] >>> print `a` 'None'
3>>> a = test[7] >>> print `a` 'None'
['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 True6
Result of add function is 5 Result of is_true function is True4
def fun[]: statements . . return [expression]3
Result 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
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True4
geeksforgeeks 204
def test[val]:
if 0 == val:
return 8
else:
return None
4 ________ 66 & NBSP;>>> a = test[7]
>>> print `a`
'None'
0 geeksforgeeks 208
>>> 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 True9
def test[val]:
if 0 == val:
return 8
else:
return None
9def 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 True6
Result of add function is 5 Result of is_true function is True4
def fun[]: statements . . return [expression]3
Result 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
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True4
geeksforgeeks 204
def test[val]:
if 0 == val:
return 8
else:
return None
4 ________ 66 & NBSP;>>> a = test[7]
>>> print `a`
'None'
0 geeksforgeeks 208
>>> 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'
20def 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 True6
Result of add function is 5 Result of is_true function is True4
def fun[]: statements . . return [expression]3
Result 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
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True4
geeksforgeeks 204
def test[val]:
if 0 == val:
return 8
else:
return None
4 ________ 66 & NBSP;>>> a = test[7]
>>> print `a`
'None'
0 geeksforgeeks 208
>>> 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 True
9 def test[val]:
if 0 == val:
return 8
else:
return None
4 geeksforgeeks
20
1
>>> a = test[7]
>>> print `a`
'None'
def test[val]:
if 0 == val:
return 8
else:
return None
>>> 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 206
>>> 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 True9
The result is 25 The result is: 1004
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'
58Result of add function is 5 Result of is_true function is True6
>>> 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'
67def test[val]:
if 0 == val:
return 8
else:
return None
4 >>> a = test[7]
>>> print `a`
'None'
69>>> a = test[7]
>>> print `a`
'None'
70def test[val]:
if 0 == val:
return 8
else:
return None
9def 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'
89def test[val]:
if 0 == val:
return 8
else:
return None
3def test[val]:
if 0 == val:
return 8
else:
return None
4 >>> a = test[7]
>>> print `a`
'None'
92def 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'
77Output:
The result is 25 The result is: 100