0

我正在尝试打印这些car_object[objectname]对象,但不知道该怎么做……我还有一个 Cars 类。当我这样做时,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)
4

1 回答 1

1

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

class Cars:
    # ....

    def __str__(self):
        return 'Car instance with variables: {!r}'.format(self.variables)
于 2013-02-28T21:42:38.580 回答