高阶 python 函数并正确设置它们的属性
Higher order python functions and setting their attributes correctly
我目前正在编写一个函数,使 returns 成为一个创建多项式表达式的新函数。我希望函数以字符串形式存储多项式的系数和多项式本身。但是,如果解释器坚持新创建的函数没有这样的属性,我似乎无法设置任何一个属性。
请看下面我的函数
def poly(coefs):
"""Return a function that represents the polynomial with these coefficients.
For example, if coefs=(10, 20, 30), return the function of x that computes
'30 * x**2 + 20 * x + 10'. Also store the coefs on the .coefs attribute of
the function, and the str of the formula on the .__name__ attribute.'"""
# your code here (I won't repeat "your code here"; there's one for each function)
def createPoly(x):
formulaParts = []
power = 0
createPoly.coefs = coefs
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power +=1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.value = eval(createPoly.__name__)
return createPoly.value
return createPoly
如您所见,我在上面的代码中设置属性并使用它们时没有问题。但是,如果我使用如下代码,则会发生错误
y = poly((5,10,5))
print(y.__name__)
这可能是我忽略的非常简单的事情。请帮忙
您设置内部函数的代码不能在内部函数内部:
def poly(coefs):
def createPoly(x):
createPoly.value = eval(createPoly.__name__)
return createPoly.value
formulaParts = []
power = 0
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power += 1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.coefs = coefs
return createPoly
我目前正在编写一个函数,使 returns 成为一个创建多项式表达式的新函数。我希望函数以字符串形式存储多项式的系数和多项式本身。但是,如果解释器坚持新创建的函数没有这样的属性,我似乎无法设置任何一个属性。
请看下面我的函数
def poly(coefs):
"""Return a function that represents the polynomial with these coefficients.
For example, if coefs=(10, 20, 30), return the function of x that computes
'30 * x**2 + 20 * x + 10'. Also store the coefs on the .coefs attribute of
the function, and the str of the formula on the .__name__ attribute.'"""
# your code here (I won't repeat "your code here"; there's one for each function)
def createPoly(x):
formulaParts = []
power = 0
createPoly.coefs = coefs
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power +=1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.value = eval(createPoly.__name__)
return createPoly.value
return createPoly
如您所见,我在上面的代码中设置属性并使用它们时没有问题。但是,如果我使用如下代码,则会发生错误
y = poly((5,10,5))
print(y.__name__)
这可能是我忽略的非常简单的事情。请帮忙
您设置内部函数的代码不能在内部函数内部:
def poly(coefs):
def createPoly(x):
createPoly.value = eval(createPoly.__name__)
return createPoly.value
formulaParts = []
power = 0
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power += 1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.coefs = coefs
return createPoly