我在python中有一个类,该类将需要对对象进行计算,这些对象会从循环的列表中获取数据,并每次都从csv添加新行。
如何将__init__()
中定义的对象调用到calcA()
,calcB()
,calcC()
方法中,以对其中存储的每一行进行计算,然后将结果返回到Calculation
中定义的对象,而不更改已经存储在__init__
中的数据?
然后我将使用__init__
和Calculation
中的对象写入csv
中的列
class CreateObjects():
def __init__(self, OjectID, ObjectPrice, ObjectNum, Objectzone, ObjectTicket, Multiplier):
self.OjectID= OjectID
self.ObjectPrice= ObjectPrice
self.ObjectTicket= ObjectTicket
self.ObjectNum= ObjectNum
self.Objectzone= Objectzone
self.Multiplier = Multiplier
def Calculation(self, A, B, C):
self.A = A
self.B = B
self.C = C
def calcA():
pass
def calcB():
pass
def calcC():
pass
答案 0 :(得分:0)
假设calcA()
是一个方法,则它应具有第一个参数(习惯上称为self
)。您可以使用它来访问类实例的所有属性/属性。
例如:
class CreateObjects:
...
def calcA(self):
# this will allways work, because '__init__()' is allways
# called before this method
print(self.OjectID)
# this will only work if the method 'Calculation()' was called
# before this, because otherwise 'A' will not be defined yet
print(self.A)