在python中打印对象

时间:2013-02-28 21:34:44

标签: python python-3.x

我正在尝试打印这些car_object[objectname]个对象,但不知道该怎么做.... 我也有一个汽车课。当我print(car_object[objectname])时,我得到ObjectmeA160 <__main__.Cars object at 0x027FB970>。我做错了什么?

def __iter__(self):
    car_object = {}
    cursor = self._db.execute('SELECT IDENT, MAKE, MODEL, DISPLACEMENT, 
      POWER, LUXURY FROM CARS')
    for row in cursor:
        car_object = {}
        objectname = 'Object'+str(row['IDENT'])
        car_object[objectname] = Cars(ident = row['IDENT'], make = row['MAKE'], 
                  model = row['MODEL'], disp = row['DISPLACEMENT'], power = row['POWER'], luxury = row['LUXURY'])
        print(car_object[objectname])
        yield dict(row)

class Cars:  
    def __init__(self, **kwargs):
        self.variables = kwargs

    def set_Variable(self, k, v):
        self.variables[k] = v

    def get_Variable(self, k):
        return self.variables.get(k, None)

1 个答案:

答案 0 :(得分:1)

<__main__.Cars object at 0x027FB970>是未实现自己的.__str__() hook的自定义对象的标准字符串。您可以通过实现该方法来自定义它:

class Cars:
    # ....

    def __str__(self):
        return 'Car instance with variables: {!r}'.format(self.variables)