Python 为什么这些函数在字典中运行
我是python初学者,正在练习一门简单的计算课 这段代码假设当用户在命令行中输入2个数字和1个运算符时,它将向您显示答案。 我只是想知道为什么它在函数add()、subtract()、multiply()和divide()中打印4行。 我只是把它们放在字典里,而不是把它们都记下来。 谁能给我解释一下吗?如果能给我展示一下解决方案,那就太好了。 提前谢谢 以下是windows power shell的输出:Python 为什么这些函数在字典中运行,python,dictionary,Python,Dictionary,我是python初学者,正在练习一门简单的计算课 这段代码假设当用户在命令行中输入2个数字和1个运算符时,它将向您显示答案。 我只是想知道为什么它在函数add()、subtract()、multiply()和divide()中打印4行。 我只是把它们放在字典里,而不是把它们都记下来。 谁能给我解释一下吗?如果能给我展示一下解决方案,那就太好了。 提前谢谢 以下是windows power shell的输出: PS D:\misc\Code\python\mystuff> python .\
PS D:\misc\Code\python\mystuff> python .\CalculationTest.py
Please input a number:
>1
Please input a operator(i.e. + - * /):
>+
Please input another number:
>2
Adding 1 + 2 #why it shows these 4 lines?
Subtracting 1 - 2
Multiplying 1 * 2
Dividing 1 / 2
3
这是我的代码:
class Calculation(object):
def add(self, a, b):
print "Adding %d + %d" % (a, b)
return a + b
def subtract(self, a, b):
print "Subtracting %d - %d" % (a, b)
return a - b
def multiply(self, a, b):
print "Multiplying %d * %d" % (a, b)
return a * b
def divide(self, a, b):
if b == 0:
print "Error"
exit(1)
else:
print "Dividing %d / %d" % (a, b)
return a / b
def get_result(self, a, b, operator):
operation = {
"+" : self.add(a, b), # I didn't mean to call these methods,
"-" : self.subtract(a, b), # but it seems that they ran.
"*" : self.multiply(a, b),
"/" : self.divide(a, b),
}
print operation[operator]
if __name__ == "__main__":
print "Please input a number:"
numA = int(raw_input(">"))
print "Please input a operator(i.e. + - * /):"
operator = raw_input(">")
print "Please input another number:"
numB = int(raw_input(">"))
calc = Calculation()
#print calc.add(numA, numB)
calc.get_result(numA, numB, operator)
你说你不是有意要调用这些方法。但你做到了。你写
self.add(a, b)
这会调用add
,因为它使用了call操作符()
。在填充字典时,您正在调用每个算术运算符方法
如果要捕获该方法而不是调用它,则需要将self.add
放入dict
中
然后,当您确实要调用该方法时,您可以这样做:
print operation[operator](a, b)
def get_result(self, a, b, operator):
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
print operation[operator](a, b)
此时,我们正在使用call操作符()
,并提供参数
综上所述,您的函数如下所示:
print operation[operator](a, b)
def get_result(self, a, b, operator):
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
print operation[operator](a, b)
由于dict从不变化,因此将其作为类属性并初始化一次可能更明智
在我看来,您的实例在这里并不是很有用。您在任何地方都没有提到
self
。这表明这些方法可能更好。您说您不是有意调用这些方法。但你做到了。你写
self.add(a, b)
这会调用add
,因为它使用了call操作符()
。在填充字典时,您正在调用每个算术运算符方法
如果要捕获该方法而不是调用它,则需要将self.add
放入dict
中
然后,当您确实要调用该方法时,您可以这样做:
print operation[operator](a, b)
def get_result(self, a, b, operator):
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
print operation[operator](a, b)
此时,我们正在使用call操作符()
,并提供参数
综上所述,您的函数如下所示:
print operation[operator](a, b)
def get_result(self, a, b, operator):
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
print operation[operator](a, b)
由于dict从不变化,因此将其作为类属性并初始化一次可能更明智
在我看来,您的实例在这里并不是很有用。您在任何地方都没有提到self
。这表明这些方法可能更好,因为可以这样做:
def get_result(self, a, b, operator):
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
print operation[operator](a, b)
注意在创建字典后实际的方法调用(使用(a,b)
)是如何移动到的。按照您的方式,您正在调用每个方法并将结果存储在字典中,而不是存储方法然后只调用所需的方法。请执行以下操作:
def get_result(self, a, b, operator):
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
print operation[operator](a, b)
注意在创建字典后实际的方法调用(使用(a,b)
)是如何移动到的。按照您的方式,您正在调用每个方法并将结果存储在字典中,而不是存储方法然后只调用所需的方法。以下代码:
operation = {
"+" : self.add(a, b), # I didn't mean to call these methods,
"-" : self.subtract(a, b), # but it seems that they ran.
"*" : self.multiply(a, b),
"/" : self.divide(a, b),
}
将热切地评估所有值,因为这是python的工作方式。您要执行以下操作:
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
然后调整代码的其余部分,使其仅调用适当的方法
如果要以原始样式编写代码,则需要一种具有惰性计算语义的语言,如haskell。以下代码:
operation = {
"+" : self.add(a, b), # I didn't mean to call these methods,
"-" : self.subtract(a, b), # but it seems that they ran.
"*" : self.multiply(a, b),
"/" : self.divide(a, b),
}
将热切地评估所有值,因为这是python的工作方式。您要执行以下操作:
operation = {
"+" : self.add,
"-" : self.subtract,
"*" : self.multiply,
"/" : self.divide,
}
然后调整代码的其余部分,使其仅调用适当的方法
如果您想以原始风格编写代码,则需要一种具有惰性计算语义的语言,如haskell