python TypeError:dl()正好接受4个参数(给定3个)

python TypeError:dl()正好接受4个参数(给定3个),python,class,urllib,Python,Class,Urllib,我收到一个类型错误,我的类在其函数dl()中使用self 当运行时,我得到类型错误,但我在类函数dl中有self,我做错了什么 >>> s = sai_download() >>> s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python") Traceback (most recent call last): File

我收到一个类型错误,我的类在其函数dl()中使用self

当运行时,我得到类型错误,但我在类函数dl中有self,我做错了什么

>>> s = sai_download()
>>> s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python")

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python")
TypeError: dl() takes exactly 4 arguments (3 given)
>>>  
>s=sai_下载()
>>>s.dl(“C:\hh.html”http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python")
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
s、 dl(“C:\hh.html”http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python")
TypeError:dl()正好接受4个参数(给定3个)
>>>  

您需要定义pck参数

s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python", True)
或者,如果要使用默认值使参数成为可选参数,请定义如下方法:

def dl(self,_dir,_url,pck=True):

您需要定义pck参数

s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python", True)
或者,如果要使用默认值使参数成为可选参数,请定义如下方法:

def dl(self,_dir,_url,pck=True):

作为旁注,所有那些
==True
比较都是不必要的;只要使用
(如果存在)(\u url):
。或者,在上一个示例中,只返回os.path.isfile(_dir)。此外,将所有参数命名为带有下划线前缀的“private”变量也很奇怪。defdl(self、dir、url、pck)有什么问题吗??看起来您借用了一种为其他语言(可能是带有隐式
self
,或JS风格的隐式全局变量)而设计的习惯用法,这在Python中并不合适,只会降低代码的可读性。另外,在缩进块之前需要一个空行来将其格式化为代码。(我已经为您解决了这个问题,但供将来参考…)作为旁注,所有这些
==True
比较都是不必要的;只要使用
(如果存在)(\u url):
。或者,在上一个示例中,只返回os.path.isfile(_dir)。此外,将所有参数命名为带有下划线前缀的“private”变量也很奇怪。defdl(self、dir、url、pck)有什么问题吗??看起来您借用了一种为其他语言(可能是带有隐式
self
,或JS风格的隐式全局变量)而设计的习惯用法,这在Python中并不合适,只会降低代码的可读性。另外,在缩进块之前需要一个空行来将其格式化为代码。(我已经为您解决了这个问题,但仅供将来参考…)