Class Python类没有按预期工作?
下面的代码给出了周日的结果0。没有工作,但周日有5个工作Class Python类没有按预期工作?,class,python-2.7,Class,Python 2.7,下面的代码给出了周日的结果0。没有工作,但周日有5个工作 sunday = workbook.sheet_by_name('Sunday') class Day: def __init__(self, day): self.day = day jobs = {} def getjobs(self, day): #populates a dictionary with the information from an excel sheet
sunday = workbook.sheet_by_name('Sunday')
class Day:
def __init__(self, day):
self.day = day
jobs = {}
def getjobs(self, day): #populates a dictionary with the information from an excel sheet
for i in range(start_row,day.nrows):
self.jobs[i-1] = [str(day.cell_value(i,0)).upper(), str(day.cell_value(i,1)).upper(), str(day.cell_value(i,2)).upper(), str(day.cell_value(i,3)).upper(), str(xlrd.xldate_as_tuple(day.cell_value(i,4),0)[3])+":"+str(xlrd.xldate_as_tuple(day.cell_value(i,4),0)[4]), str(day.cell_value(i,5)).upper(), str(day.cell_value(i,6)).upper(), str(day.cell_value(i,7)).upper(), str(day.cell_value(i,8)).upper(), str(day.cell_value(i,9)).upper(), str(day.cell_value(i,10)).upper(), str(day.cell_value(i,11)).upper(), str(day.cell_value(i,12)).upper(), str(day.cell_value(i,13)).upper(), str(day.cell_value(i,14)).upper(), str(day.cell_value(i,15)).upper(), str(day.cell_value(i,16)).upper(), str(day.cell_value(i,17)).upper(), str(day.cell_value(i,18)).upper()]
no_jobs = len(jobs)
Sunday = Day(sunday)
print Sunday.day
Sunday.getjobs(Sunday.day)
print Sunday.jobs[1][1]
print Sunday.no_jobs
如果我这样做,这是可行的:
print len(Sunday.jobs)
我如何修改课堂,使print Sunday.no_作业产生5个?您需要使用self关键字设置课堂日的变量属性: 或者,将“无作业”设置为属性:
@property
def no_jobs(self):
return len(self.jobs)
Python和糟糕的缩进不是很好的组合。你能编辑你的问题,这样我们至少可以知道类的主体在哪里结束吗?修正你的缩进。没有任何作业具有空作业的长度。在初始化作业后放置no_jobs=lenjobs,并为它们设置正确的值,如jobs=getjobsSunday;no_jobs=lenjobs抱歉,粘贴时出错!
@property
def no_jobs(self):
return len(self.jobs)