我有一个包含一些信息的python类。我有另一个文件,其中一些功能引用。我的get_date
工作正常,但我的其他功能似乎都没有起作用。调用时间函数时,我收到错误AttributeError: PVData instance has no attribute 'time'
。
class PVData:
def __init__(self):
self.date = yesterday()
self.data = load_data(self.date)
def change_date(self, date):
if self.date != date:
self.date = date
## self.refresh()
else:
self.date = date
self.date = load_data(self.date)
#time, temp, sun
self.time = []
self.temperature = []
self.sunlight = []
for minute in self.date:
self.time.append(minute[0])
self.temperature.append(minute[1])
self.sunlight.append(minute[2])
#power
self.dictonary[a] = []
for a in ARRAYS:
self.dictionary[ARRAYS[i]].append(power)
def get_date(self):
return self.date
def get_time(self, time_index):
return self.time[time_index]
def get_temperature(self):
return self.temperature
def get_sunlight(self):
return self.sunlight
def get_power(self, array):
return self.dictionary[array]
pvd = PVData()
load_data函数是(在另一个文件中):
def load_data(dateStr):
text = get_data_for_date(dateStr)
data = []
for line in text.splitlines():
time, temp, sun, powerStr = line.split(',', 3)
power = []
for p in powerStr.split(','):
power.append(int(p))
data.append((time, float(temp), float(sun), tuple(power)))
return data
返回如下内容:
[('19:00', 20.0, 0.0, (0, 0, 0, 0, 0, 0, 0, 21, 31, 52)), (tuple 2), etc etc]
错误似乎是因为时间不是自我的有效参数,但我认为这是self.time = []
所定义的。
请原谅我缺乏知识,python对我来说很新鲜。为什么这不符合要求的任何想法?
答案 0 :(得分:0)
将time
以及应从外部访问的其他变量移至def init(self)
。请记住,python在运行时创建变量,所以如果你想在任何地方都可以访问变量 - 它们应该在类初始化时创建。
添加了:
从您的代码看起来,您应该将temperature
和sunlight
移至def init(self)
。