engr007
engr007

Reputation: 51

printing objects in python

I'm trying to print these car_object[objectname] objects, but not sure how to do it.... I also have a Cars class. When I do print(car_object[objectname]) I get ObjectmeA160 <__main__.Cars object at 0x027FB970>. what am I doing wrong?

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)

Upvotes: 0

Views: 373

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125398

The <__main__.Cars object at 0x027FB970> is the standard string for custom objects that do not implement their own .__str__() hook. You can customize it by implementing that method:

class Cars:
    # ....

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

Upvotes: 1

Related Questions