如何将字典值传递给 python 的不同 类 中的算术函数
how to pass dictionary value to arithmetic functions in different classes of python
I am trying to pass a dictionary value to a simple arithmetic
functions. I am using 3 classes.ClassA have add() sub() mul() div() functions defined.Class B contain only values used for operations. Class c having some checks.ClassA look like these.
class ClassA(object):
def addition(self, x, y):
return x + y
def substraction(self, x, y):
return x - y
def division(self, x,y):
return x / y
def multiplication(self, x, y):
return x * y
ClassB looks like this.
class B类(对象):
def __init__(self):
self.var1 = 2
self.var2 = 8
and here is ClassC
from File1 import ClassA
from File2 import ClassB
class ClassC(ClassB):
def __init__(self,ClassB):
self.var3= ClassB.var1
self.var4= ClassB.var2
print(self.var3)#testing
print(self.var4)#testing
def checkWholeNumber(self):
if self.var3%2 ==0 or (self.var3+1)%2 & self.var4%2 == 0 or (self.var4+1)%2:
print(self.var3) #if value is whole then it will print some value or will throw exception
return True
return False
def addDictAndAss(self):
myDict={}
myDict["x"]=self.var3
myDict["y"]=self.var4
print(myDict) #to check whether values are added or not
object1 = ClassB()
object2 = ClassC(object1)
object2.checkWholeNumber()
object2.addDictAndAss()
object3= ClassA()
object3.addition(*myDict)
Now I want to assign my created dictionary to functions of the classA. Thanks in advance.
Edit1:File1.py contain ClassA ,File2.py contain ClassB, File3.py contain ClassC
#第一步:
class ClassA(object):
def addition(x, y):
X=x
Y=y
return X + Y
def substraction(x, y):
X = x
Y = y
return X - Y
def division(x, y):
X = x
Y = y
return X / Y
def multiplication(x, y):
X = x
Y = y
return X * Y
#step2:
class ClassB(object):
def __init__(self):
self.var1 = 10
self.var2 = 8
#Step3:
from File1 import ClassA
from File2 import ClassB
class ClassC(ClassB):
def __init__(self,ClassB):
self.var3= ClassB.var1
self.var4= ClassB.var2
#print(self.var3)#testing
#print(self.var4)#testing
def checkWholeNumber(self):
if self.var3%2 ==0 or (self.var3+1)%2 & self.var4%2 == 0 or (self.var4+1)%2:
# print(self.var3) #if value is whole then it will print some value or will throw exception
return True
return False
def addDictAndAss(self):
myDict={}
myDict["x"]=self.var3
myDict["y"]=self.var4
#print(myDict) #to check whether values are added or not
print("Please select one arithmatic operator i.e. '+','-','/','*'")
operator=input()
if operator == '+':
add=ClassA.addition(**myDict)
print(add)
elif operator == '-':
sub=ClassA.substraction(**myDict)
print(sub)
elif operator == '/':
div=ClassA.division(**myDict)
print(div)
elif operator == '*':
mul=ClassA.multiplication(**myDict)
print(mul)
# Creating object and calling(assiging)
object1 = ClassB()
object2 = ClassC(object1)
object2.checkWholeNumber()
object2.addDictAndAss()
object3= ClassA()
请提出更好的方法。
I am trying to pass a dictionary value to a simple arithmetic functions. I am using 3 classes.ClassA have add() sub() mul() div() functions defined.Class B contain only values used for operations. Class c having some checks.ClassA look like these.
class ClassA(object):
def addition(self, x, y):
return x + y
def substraction(self, x, y):
return x - y
def division(self, x,y):
return x / y
def multiplication(self, x, y):
return x * y
ClassB looks like this.
class B类(对象):
def __init__(self):
self.var1 = 2
self.var2 = 8
and here is ClassC
from File1 import ClassA
from File2 import ClassB
class ClassC(ClassB):
def __init__(self,ClassB):
self.var3= ClassB.var1
self.var4= ClassB.var2
print(self.var3)#testing
print(self.var4)#testing
def checkWholeNumber(self):
if self.var3%2 ==0 or (self.var3+1)%2 & self.var4%2 == 0 or (self.var4+1)%2:
print(self.var3) #if value is whole then it will print some value or will throw exception
return True
return False
def addDictAndAss(self):
myDict={}
myDict["x"]=self.var3
myDict["y"]=self.var4
print(myDict) #to check whether values are added or not
object1 = ClassB()
object2 = ClassC(object1)
object2.checkWholeNumber()
object2.addDictAndAss()
object3= ClassA()
object3.addition(*myDict)
Now I want to assign my created dictionary to functions of the classA. Thanks in advance. Edit1:File1.py contain ClassA ,File2.py contain ClassB, File3.py contain ClassC
#第一步:
class ClassA(object):
def addition(x, y):
X=x
Y=y
return X + Y
def substraction(x, y):
X = x
Y = y
return X - Y
def division(x, y):
X = x
Y = y
return X / Y
def multiplication(x, y):
X = x
Y = y
return X * Y
#step2:
class ClassB(object):
def __init__(self):
self.var1 = 10
self.var2 = 8
#Step3:
from File1 import ClassA
from File2 import ClassB
class ClassC(ClassB):
def __init__(self,ClassB):
self.var3= ClassB.var1
self.var4= ClassB.var2
#print(self.var3)#testing
#print(self.var4)#testing
def checkWholeNumber(self):
if self.var3%2 ==0 or (self.var3+1)%2 & self.var4%2 == 0 or (self.var4+1)%2:
# print(self.var3) #if value is whole then it will print some value or will throw exception
return True
return False
def addDictAndAss(self):
myDict={}
myDict["x"]=self.var3
myDict["y"]=self.var4
#print(myDict) #to check whether values are added or not
print("Please select one arithmatic operator i.e. '+','-','/','*'")
operator=input()
if operator == '+':
add=ClassA.addition(**myDict)
print(add)
elif operator == '-':
sub=ClassA.substraction(**myDict)
print(sub)
elif operator == '/':
div=ClassA.division(**myDict)
print(div)
elif operator == '*':
mul=ClassA.multiplication(**myDict)
print(mul)
# Creating object and calling(assiging)
object1 = ClassB()
object2 = ClassC(object1)
object2.checkWholeNumber()
object2.addDictAndAss()
object3= ClassA()
请提出更好的方法。