我创建了一个函数,我想在列表中附加一个数字:
variableToChange[0] = true;
为什么我必须返回一个功能?它可以使用和不使用def num(f):
list1.append(i)
return list1
list1 = []
i = 1
print "Now list1 is %s and i is %d" % (list1, i)
num(list1)
i += 1
print "Now list1 is %s and i is %d" % (list1, i)
num(list1)
i += 1
print "Now list1 is %s and i is %d" % (list1, i)
print list1
print i
。
如果没有达到return语句,我被告知该函数返回return
。但是,即使我没有输入这个返回语句,上面提到的函数仍然有效。
答案 0 :(得分:2)
在提供的示例中,返回值是不必要的,因为append
列表方法以及您的函数也可以通过副作用进行操作。仅为副作用调用的函数不是数学意义上的函数(域值到codomain值的映射),它们用于更改可变对象的状态,在这种情况下,通过将项附加到列表。虽然这些函数可以返回一个值,但是Python有一个约定,该函数纯粹为了副作用调用,例如list.append
,只返回None
,表示没有有用的返回值。 / p>
如果你的函数没有副作用,你需要有一个return语句才能使它有用。例如,比较:
def add(a, b):
return a + b
......在语法上同样合法,但很无用:
def add(a, b):
a + b
# without a return statement, None is returned,
# and the calculated sum discarded
答案 1 :(得分:2)
我发现你不了解函数是如何工作的,所以我在你的代码中添加了一些注释来解释一下,但我建议你进一步阅读Python tutorial about functions和wiki article以获得理解。
另外,我省略了许多细节,不会使解释过载。重要的是有不可变的(即你的例子中的整数,i
)和Python中的可变(即你的例子中的列表,list1
类型,并且根据这种情况,行为会有所不同。
def num(f):
#Here the argument you pass to the function is named 'f'
#and you don't use it
#The next line uses 'list1', that is defined in global scope
#since you didn't redefined this name inside the function
#Variable 'i' is also the one in global scope for same reasons
list1.append(i)
#Here you return 'list1', though you don't use this value
#further in your program. Indeed, you would not write a return
#statement the function would return 'None' as the return value
return list1
#Here you define 'list1' in global scope, and it will be used
#inside 'num' function, even without providing it as the argument
list1 = []
#Here you define 'i' in global scope, and it will be used
#inside 'num' function
i = 1
#Here you print 'i' and 'list' from global scope
print "Now list1 is %s and i is %d" % (list1, i)
#Here you call 'num' function and 'list1' provided as argument
#is assigned to 'f' inside the function, but you didn't used it and
#and instead used names from global scope - that's why it works in
#this way (however it is wrong use of function)
#With 'list1.append(i)' the 'list1' is modified in place so it
#doesn't matter if it is returned or not
num(list1)
#As to 'num' return value, it will be the same value as 'list1', but
#you don't use it here, to use it it needs to be assigned with '=' to
#some variable, i.e. 'list2=num(list1)', though in fact 'list1' and 'list2'
#will be the same all the time due to Python internals, but let's skip the
#details of this.
#You can see that the value returned is not 'None' - add the
#following line here:
print(num(list1))
#and run your program, the output will show you that it's a list returned.
#then remove the 'return' line in your function and run program again
#the output here will show, that is's 'None' that was returned.
所以要解决函数中明显的错误:
def num(f):
f.append(i)
return f
但是我仍然在全局范围内使用而不是作为参数传递,所以更好:
def num(f_var,i_var):
f_var.append(i_var)
return f_var
虽然该列表将在适当的位置进行修改,但您并不需要将其返回 在你的特定例子中,所以:
def num(f_var,i_var):
f_var.append(i_var)
list1=[]
i=1
num(list1,i)
也会奏效。
答案 2 :(得分:1)
你没有使用返回值,所以它没有区别,你返回的是什么。你也没有使用这个论点。你可能想写
def append_num(f, i):
f.append(i)
并使用两个参数:
append_num(list1, i)