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 初始化类时,AttributeError:';int';对象没有属性';[参数名称]';_Python_Class - Fatal编程技术网

Python 初始化类时,AttributeError:';int';对象没有属性';[参数名称]';

Python 初始化类时,AttributeError:';int';对象没有属性';[参数名称]';,python,class,Python,Class,在Python中,定义在其\uuuu init\uuuu方法中接受参数的类时: class animal: number_of_legs = 0 def __init__(nlegs, self): self.number_of_legs = nlegs a = animal(3) 我得到以下错误: AttributeError:“int”对象没有“腿数”属性 更改: def __init__(nlegs, self): 致: 因为在您的代码实例中,被分配给nlegs

在Python中,定义在其
\uuuu init\uuuu
方法中接受参数的类时:

class animal:  
  number_of_legs = 0

  def __init__(nlegs, self):
    self.number_of_legs = nlegs

a = animal(3)
我得到以下错误:

AttributeError:“int”对象没有“腿数”属性

更改:

def __init__(nlegs, self):
致:

因为在您的代码实例中,被分配给
nlegs
,而3被分配给
self

您应该将
self
作为类方法中的第一个参数

def __init__(self, nlegs):