Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么时候应该使用函数?_Python_Python 3.x_Python 2.7_Python 3.6 - Fatal编程技术网

Python 什么时候应该使用函数?

Python 什么时候应该使用函数?,python,python-3.x,python-2.7,python-3.6,Python,Python 3.x,Python 2.7,Python 3.6,我想知道,什么时候应该使用函数,因为我看到很多人使用函数来继续代码,而不是解析参数并返回结果 我指的是这样的功能: def foo(): print("How are you?") print("Hi.") foo() (这段代码没有意义,也不实用。它只是一个例子。) 为什么我们要使用函数,而不是将代码放在一个连续的地方?什么更容易阅读和维护 第1版: db = init_database() connect_to_database(db) target = 'funny_cats'

我想知道,什么时候应该使用函数,因为我看到很多人使用函数来继续代码,而不是解析参数并返回结果

我指的是这样的功能:

def foo():
   print("How are you?")

print("Hi.")
foo()
(这段代码没有意义,也不实用。它只是一个例子。)


为什么我们要使用函数,而不是将代码放在一个连续的地方?

什么更容易阅读和维护

第1版:

db = init_database()
connect_to_database(db)
target = 'funny_cats'
cats = db.fetch(target)
for user in lots_of_users:
    if not polite(user):
        ban(user)
第2版:

5000 lines of code resembling the bodies of the above function calls
for user in lots_of_users:
    30 lines of code for body of polite
    30 lines of code for body of ban


什么更容易阅读和维护

第1版:

db = init_database()
connect_to_database(db)
target = 'funny_cats'
cats = db.fetch(target)
for user in lots_of_users:
    if not polite(user):
        ban(user)
第2版:

5000 lines of code resembling the bodies of the above function calls
for user in lots_of_users:
    30 lines of code for body of polite
    30 lines of code for body of ban

假设您希望在程序中的不同位置执行同一段代码

some_function()
[100 lines]
some_function()
[100 lines]
[conditional branch possibly calling some_function()]
对于函数,只需在所需位置调用该函数。如果没有函数,您需要到处复制代码。当您想要更改这些代码块的行为时,您必须查找复制代码的所有位置,而不仅仅是调整一个函数。如果您错过了一个位置,您将引入一个bug


函数是Python中的第一类对象,这是一种花哨的说法,因为它们的行为与任何其他更普通的对象一样——您可以将它们放在列表中,或者将它们用作其他函数的参数

这允许您将函数视为可由其他代码动态执行的自包含代码段,例如通过将排序条件传递给内置的
sorted
函数

>>> def criterion(sequence):
...     return sequence[-1]
... 
>>> list_of_sequences = [(1, 2), (5, -1), (2, 10)]
>>> 
>>> sorted(list_of_sequences, key=criterion)
[(5, -1), (1, 2), (2, 10)]

什么更容易阅读和维护

第1版:

db = init_database()
connect_to_database(db)
target = 'funny_cats'
cats = db.fetch(target)
for user in lots_of_users:
    if not polite(user):
        ban(user)
第2版:

5000 lines of code resembling the bodies of the above function calls
for user in lots_of_users:
    30 lines of code for body of polite
    30 lines of code for body of ban


什么更容易阅读和维护

第1版:

db = init_database()
connect_to_database(db)
target = 'funny_cats'
cats = db.fetch(target)
for user in lots_of_users:
    if not polite(user):
        ban(user)
第2版:

5000 lines of code resembling the bodies of the above function calls
for user in lots_of_users:
    30 lines of code for body of polite
    30 lines of code for body of ban

假设您希望在程序中的不同位置执行同一段代码

some_function()
[100 lines]
some_function()
[100 lines]
[conditional branch possibly calling some_function()]
对于函数,只需在所需位置调用该函数。如果没有函数,您需要到处复制代码。当您想要更改这些代码块的行为时,您必须查找复制代码的所有位置,而不仅仅是调整一个函数。如果您错过了一个位置,您将引入一个bug


函数是Python中的第一类对象,这是一种花哨的说法,因为它们的行为与任何其他更普通的对象一样——您可以将它们放在列表中,或者将它们用作其他函数的参数

这允许您将函数视为可由其他代码动态执行的自包含代码段,例如通过将排序条件传递给内置的
sorted
函数

>>> def criterion(sequence):
...     return sequence[-1]
... 
>>> list_of_sequences = [(1, 2), (5, -1), (2, 10)]
>>> 
>>> sorted(list_of_sequences, key=criterion)
[(5, -1), (1, 2), (2, 10)]

一个例子:打几次电话时应该用。一个例子:打几次电话时应该用。回答得很好,谢谢,你的第一个例子不是我要找的答案。但是你的第二个例子非常有用。非常好的答案,谢谢你,你的第一个例子不是我想要的答案。但是你的第二个例子非常有用。