Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 编写类的函数,该函数将构建一个字符串,该字符串保存有关类对象的所有信息_Python_List - Fatal编程技术网

Python 编写类的函数,该函数将构建一个字符串,该字符串保存有关类对象的所有信息

Python 编写类的函数,该函数将构建一个字符串,该字符串保存有关类对象的所有信息,python,list,Python,List,我想编写一个类的方法,并将其称为dumpData,它将构建一个包含有关对象的所有信息的字符串。我尝试了几个代码,但得到的都是AttributeError:“list”对象没有属性“dumpData” 这是我迄今为止编写的代码: class Car(): def __init__(self, brand, productionYear) self.brand = brand self.productionYear = productionYear

我想编写一个类的方法,并将其称为
dumpData
,它将构建一个包含有关对象的所有信息的字符串。我尝试了几个代码,但得到的都是
AttributeError:“list”对象没有属性“dumpData”

这是我迄今为止编写的代码:

class Car(): 

    def __init__(self, brand, productionYear) 
        self.brand = brand
        self.productionYear = productionYear

    def dumpData(self,CarList1):
             return CarList1

 if __name__ =="__main__":
    carObject = []
    for i in range(10):
         carObjectList = []

         brand_list = ['kia', 'Hunday', 'BMW', 'Audi', 'Jeep']
         brand = random.choice(brand_list)

         productionYear = random.randint(1995, 2020)               
         carObject.append(Car(brand, productionYear))

         carObjectList.append(carObject)

  print(carObject.dumpData(carObjectList))
我编辑了这个问题,因为它似乎不够清楚。
提前感谢

您的错误是您有一个列表对象,而不是您试图调用函数的类的实例


我建议让您的类实际持有列表,并使用add函数获取您想要的信息

此时,列表不需要参数

class Car():
  def __init__(self, brand, year):
    self.brand = brand
    self.year = year

  def __repr__(self):
     return self.brand + "," + str(self.year)

class CarList():
  def __init__(self):
    self.cars = []

  def dump(self):
    return str(self.cars)

  def add(self, brand, year):
    self.cars.append(Car(brand, year))

carList1 = CarList()
carList1.add('honda', 2009)

print(carList1.dump())

根据您的错误,您无法执行
[some,data].dumpData()
。请显示您正在调用的成员函数在Python中称为实例方法。
carObject
在您的代码中不存在。。。即使这样做有效,
carObjectList
也没有dumpData函数,如前所述