Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更新类中另一个函数中的变量?_Python_Python 2.7 - Fatal编程技术网

Python 如何更新类中另一个函数中的变量?

Python 如何更新类中另一个函数中的变量?,python,python-2.7,Python,Python 2.7,我有一个类,其中有几个函数,在我的change\u date函数中,我需要重新分配\u init\u中的self.date变量。但是,一开始,我需要self.date=beday(),我如何从其他函数重新分配它,然后用新的日期继续执行\uuuuuuuu init\uuuuu的过程 class PVData: def __init__(self): self.date = yesterday() self.data = load_data(self.date

我有一个类,其中有几个函数,在我的
change\u date
函数中,我需要重新分配
\u init\u
中的
self.date
变量。但是,一开始,我需要
self.date=beday()
,我如何从其他函数重新分配它,然后用新的日期继续执行
\uuuuuuuu init\uuuuu
的过程

class PVData:
    def __init__(self):
        self.date = yesterday()
        self.data = load_data(self.date)

        #time, temp, sun
        self.time = []
        self.temperature = []
        self.sunlight = []
        self.powerlist = [] ####LIST OF ALL POWER TUPLES FOR DAY
        for minute in self.data:
            self.time.append(minute[0])
            self.temperature.append(minute[1])
            self.sunlight.append(minute[2])
            self.powerlist.append(minute[3])
        ##power

        self.placelist = []
        for i in ARRAYS:
            self.placelist.append(i)

        ##Combine lists to dictionary with loop
        self.dictionary = {}
        self.dictionary = dict(zip(self.placelist, self.powerlist)) 




def change_date(self, date):
        if self.date != date:
            self.date = date
            self.__init__()           
            #self.refresh()
        else:
            self.date = date

通常,您希望将大部分
\uuuuu init\uuuuu
逻辑重构为一个不同的函数,例如
\u initialize
使用
self.date
设置其他所有功能

class PVData(object):
    def __init__(self):
        self.date = yesterday()
        self._initialize()

    def change_date(self, date):
        if self.date != self.date:
            self.date = date
            self._initialize()

    def _initialize(self):
        self.data = load_data(self.date)

        #time, temp, sun
        self.time = []
        self.temperature = []
        self.sunlight = []
        self.powerlist = [] ####LIST OF ALL POWER TUPLES FOR DAY
        for minute in self.data:
            self.time.append(minute[0])
            self.temperature.append(minute[1])
            self.sunlight.append(minute[2])
            self.powerlist.append(minute[3])
        ##power

        self.placelist = []
        for i in ARRAYS:
            self.placelist.append(i)

        ##Combine lists to dictionary with loop
        self.dictionary = {}
        self.dictionary = dict(zip(self.placelist, self.powerlist)) 

您不需要调用
self.\uuu init\uuuu()
。当您创建一个
PVData
类时,它将为您自动调用
\uuuu init\uuuu()
,而不是使用initilize函数。我是否能够利用if语句的
else
部分,然后在init中调用
change\u date