为什么我的课程要上课时,为什么需要两个参数?

时间:2016-02-21 20:08:37

标签: python class python-3.x arguments

我在Windows 10上运行Python 3.3.4

以下是我的课程代码 -

class Curl():

    def __init__(self):
        self.file7 = file7
        self.Days1 = Days1

    def readfile(self):
        ticknum = 0
        read_ticker = []
        ins = open(file7, "r" )
        for line in ins:
            if line.endswith('\n'):
                line=line[:-1]
            read_ticker.append(line)
            ticknum =+1
        ins.close()
        return read_ticker

    def CalcDates(self, Days1):  # Determine dates
        calculated_dates = dict()
        Run_Time = (time.strftime("%H/%M/%S"))
        calculated_dates['Run_Time']= Run_Time
        Today = date.today()
        calculated_dates['Today'] = Today
        End_Date = (Today - timedelta(days=Days1)) 
        calculated_dates ['Start_Date'] = Today
        Start_Day = str(Today.strftime("%d")) 
        calculated_dates['Start_Day'] = Start_Day
        Start_Month = str(Today.strftime("%m")) 
        calculated_dates['Start_Month'] = Start_Month
        Start_Year = str(Today.strftime("%Y")) 
        calculated_dates['Start_Year']= Start_Year
        End_Day = str(End_Date.strftime("%d"))
        calculated_dates['End_Day'] = End_Day
        End_Month = str(End_Date.strftime("%m"))
        calculated_dates['End_Month']= End_Month
        End_Year = str(End_Date.strftime("%Y"))
        calculated_dates['End_Year']= End_Year
        return calculated_dates

如果我执行以下操作,它会运行;

file7 = 'C:\\...\\file1.txt'
fileList = Curl.readfile(file7)
print('readTickers is complete')
print(fileList)
D1 = Curl.CalcDates( 90, 90)
print(D1)

如果我按如下方式更改D1行,我希望它能够运行;    D1 = Curl.CalcDates(90)

但它没有 - 我收到以下错误;

Traceback (most recent call last):
File "C:\Users\Edge\Desktop\readTICKR class432.py", line 56, in <module>
D1 = Curl.CalcDates(90)
TypeError: CalcDates() missing 1 required positional argument: 'Days1'

当我打电话给Curl.CalcDates时,为什么要求我提出双重论据? 我该如何修复它以便我可以使用一个争论?

1 个答案:

答案 0 :(得分:1)

CalcDates是一个实例方法的原因,因此您需要先创建一个Curl对象。

D1 = Curl(file7, Days1).CalcDates(90)

您还需要修复__init__功能。

def __init__(self, file7, Days1):
    self.file7 = file7
    self.Days1 = Days1