在python类中对对象执行计算

时间:2018-10-19 10:17:16

标签: python

我在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

1 个答案:

答案 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)