将Parent的实例变量传递给Inner类作为函数的参数

时间:2018-11-19 20:08:55

标签: python python-3.x

FactsheetExporter,我有data实例变量。我需要将data作为实例变量传递给Parameter类,但不知道如何。你知道怎么做吗?

class FactsheetExporter:
    def __init__(self):
        self.data = {somedata...}

class Parameter:
    def __init__(self):
        self.data = data

    def compute(self):
        do_stuff(self.data)
        # do data stuff...

class PortfolioFactsheetExporter(FactsheetExporter):
    class Meta(FactsheetExporter.Meta):
        name = "export_portfolio_factsheets"
        entities = Parameter()

1 个答案:

答案 0 :(得分:1)

data是类FactsheetExporter的实例变量。因此,必须将FactsheetExporter的实例发送到Parameter才能访问data

class FactsheetExporter:
    def __init__(self):
        self.data = {somedata...}

class Parameter(FactsheetExporter):
    def compute(self,FactsheetExporter_var):
         data = FactsheetExporter_var.data
         # do data stuff...


object1 = FactsheetExporter()
#Object1 should be passed to Parameter in order for it to be able to access data variable
object2 = Parameter(object1)
object2.compute(value_for_data)