Storing class objects dynamically in an array

B

Brian D

Hi,

I'm trying to instantiate a class object repeated times, dynamically for asmany times as are required, storing each class object in a container to later write out to a database. It kind of looks like what's needed is a two-dimensional class object, but I can't quite conceptualize how to do that.

A simpler approach might be to just store class objects in a dictionary, using a reference value (or table row number/ID) as the key.

In the real-world application, I'm parsing row, column values out of a table in a document which will have not more than about 20 rows, but I can't expect the document output to leave columns well-ordered. I want to be able to call the class objects by their respective row number.

A starter example follows, but it's clear that only the last instance of the class is stored.

I'm not quite finding what I want from online searches, so what recommendations might Python users make for the best way to do this?

Maybe I need to re-think the approach?


Thanks,
Brian



class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition

brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']

usedCarLot = {}

for c in range(0, len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]

for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition

0 Ford
1 Toyota
2 Fiat
0 Fiat Yellow Needs Help
1 Fiat Yellow Needs Help
2 Fiat Yellow Needs Help
 
D

Dave Angel

Hi,

I'm trying to instantiate a class object repeated times, dynamically for as many times as are required, storing each class object in a container to later write out to a database. It kind of looks like what's needed is a two-dimensional class object, but I can't quite conceptualize how to do that.

A simpler approach might be to just store class objects in a dictionary, using a reference value (or table row number/ID) as the key.

In the real-world application, I'm parsing row, column values out of a table in a document which will have not more than about 20 rows, but I can't expect the document output to leave columns well-ordered. I want to be able to call the class objects by their respective row number.

A starter example follows, but it's clear that only the last instance of the class is stored.

I'm not quite finding what I want from online searches, so what recommendations might Python users make for the best way to do this?

Maybe I need to re-think the approach?


Thanks,
Brian



class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition

brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']

usedCarLot = {}

for c in range(0, len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car

It'd work better if you actually instantiated Car, instead of just
storing the class object multiple times.

Consider:
usedCarLot[c] = Car(brandList[c], colorList[c], conditionList[c])

usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]

Don't do those 3, since you already had to supply the values when
constructing the object.

for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition


0 Ford
1 Toyota
2 Fiat
0 Fiat Yellow Needs Help
1 Fiat Yellow Needs Help
2 Fiat Yellow Needs Help

Next time, please supply the Python version, and state what you actually
expected the output to represent.
 
M

MRAB

Hi,

I'm trying to instantiate a class object repeated times, dynamically for as many times as are required, storing each class object in a container to later write out to a database. It kind of looks like what's needed is a two-dimensional class object, but I can't quite conceptualize how to do that.

A simpler approach might be to just store class objects in a dictionary, using a reference value (or table row number/ID) as the key.

In the real-world application, I'm parsing row, column values out of a table in a document which will have not more than about 20 rows, but I can't expect the document output to leave columns well-ordered. I want to be able to call the class objects by their respective row number.

A starter example follows, but it's clear that only the last instance of the class is stored.

I'm not quite finding what I want from online searches, so what recommendations might Python users make for the best way to do this?

Maybe I need to re-think the approach?


Thanks,
Brian



class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition

brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']

usedCarLot = {}

for c in range(0, len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]

for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition

0 Ford
1 Toyota
2 Fiat
0 Fiat Yellow Needs Help
1 Fiat Yellow Needs Help
2 Fiat Yellow Needs Help
You're repeatedly putting the class itself in the dict and setting its
(the class's) attributes; you're not even using the __init__ method you
defined.

What you should be doing is creating instances of the class:

for c in range(len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car(brandList[c], colorList[c], conditionList[c])
 
D

Dave Angel

Hi,

I'm trying to instantiate a class object repeated times, dynamically for as many times as are required, storing each class object in a container to later write out to a database. It kind of looks like what's needed is a two-dimensional class object, but I can't quite conceptualize how to do that.

A simpler approach might be to just store class objects in a dictionary, using a reference value (or table row number/ID) as the key.

In the real-world application, I'm parsing row, column values out of a table in a document which will have not more than about 20 rows, but I can't expect the document output to leave columns well-ordered. I want to be able to call the class objects by their respective row number.

A starter example follows, but it's clear that only the last instance of the class is stored.

I'm not quite finding what I want from online searches, so what recommendations might Python users make for the best way to do this?

Maybe I need to re-think the approach?


Thanks,
Brian



class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition

brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']

usedCarLot = {}

for c in range(0, len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]

Or even better: (untested)

for c, vals in enumerate(zip(brandList, colorList, conditionList)):
usedCarLot[c] = Car(*vals)
 
B

Brian D

Hi,

I'm trying to instantiate a class object repeated times, dynamically for as many times as are required, storing each class object in a container to later write out to a database. It kind of looks like what's needed is a two-dimensional class object, but I can't quite conceptualize how to do that..

A simpler approach might be to just store class objects in a dictionary, using a reference value (or table row number/ID) as the key.

In the real-world application, I'm parsing row, column values out of a table in a document which will have not more than about 20 rows, but I can't expect the document output to leave columns well-ordered. I want to be able to call the class objects by their respective row number.

A starter example follows, but it's clear that only the last instance of the class is stored.

I'm not quite finding what I want from online searches, so what recommendations might Python users make for the best way to do this?

Maybe I need to re-think the approach?







class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition
brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']
usedCarLot = {}
for c in range(0, len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]
for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition


0 Ford
0 Fiat Yellow Needs Help
1 Fiat Yellow Needs Help
2 Fiat Yellow Needs Help

You're repeatedly putting the class itself in the dict and setting its

(the class's) attributes; you're not even using the __init__ method you

defined.



What you should be doing is creating instances of the class:



for c in range(len(brandList)):

print c, brandList[c]

usedCarLot[c] = Car(brandList[c], colorList[c], conditionList[c])

Thanks for the quick reply Dave & MRAB. I wasn't even sure it could be done, so missing the instantiation just completely slipped.

The simplest fix is as follows, but Dave, I'll try to tighten it up a little, when I turn to the real-world code, following your enumeration example. And yes, thanks for the reminder (2.7.3). The output is fine -- I just needa record number and the list of values stored in the class object.

This is the quick fix -- instantiate class Car:

usedCarLot[c] = Car('','','')

It may not, however, be the best, most Pythonic way.

Here's the full implementation. I hope this helps someone else.

Thanks very much for the help!

class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition

brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']

#usedCarLot = {0:Car, 1:Car, 2:Car}
usedCarLot = {}

for c in range(0, len(brandList)):
#print c, brandList[c]
usedCarLot[c] = Car('','','')
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]

for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition
 
B

Brian D

Hi,

I'm trying to instantiate a class object repeated times, dynamically for as many times as are required, storing each class object in a container to later write out to a database. It kind of looks like what's needed is a two-dimensional class object, but I can't quite conceptualize how to do that..

A simpler approach might be to just store class objects in a dictionary, using a reference value (or table row number/ID) as the key.

In the real-world application, I'm parsing row, column values out of a table in a document which will have not more than about 20 rows, but I can't expect the document output to leave columns well-ordered. I want to be able to call the class objects by their respective row number.

A starter example follows, but it's clear that only the last instance of the class is stored.

I'm not quite finding what I want from online searches, so what recommendations might Python users make for the best way to do this?

Maybe I need to re-think the approach?







class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition
brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']
usedCarLot = {}
for c in range(0, len(brandList)):
print c, brandList[c]
usedCarLot[c] = Car
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]
for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition


0 Ford
0 Fiat Yellow Needs Help
1 Fiat Yellow Needs Help
2 Fiat Yellow Needs Help

You're repeatedly putting the class itself in the dict and setting its

(the class's) attributes; you're not even using the __init__ method you

defined.



What you should be doing is creating instances of the class:



for c in range(len(brandList)):

print c, brandList[c]

usedCarLot[c] = Car(brandList[c], colorList[c], conditionList[c])

Thanks for the quick reply Dave & MRAB. I wasn't even sure it could be done, so missing the instantiation just completely slipped.

The simplest fix is as follows, but Dave, I'll try to tighten it up a little, when I turn to the real-world code, following your enumeration example. And yes, thanks for the reminder (2.7.3). The output is fine -- I just needa record number and the list of values stored in the class object.

This is the quick fix -- instantiate class Car:

usedCarLot[c] = Car('','','')

It may not, however, be the best, most Pythonic way.

Here's the full implementation. I hope this helps someone else.

Thanks very much for the help!

class Car(object):

def __init__(self, Brand, Color, Condition):
self.Brand = Brand
self.Color = Color
self.Condition = Condition

brandList = ['Ford', 'Toyota', 'Fiat']
colorList = ['Red', 'Green', 'Yellow']
conditionList = ['Excellent', 'Good', 'Needs Help']

#usedCarLot = {0:Car, 1:Car, 2:Car}
usedCarLot = {}

for c in range(0, len(brandList)):
#print c, brandList[c]
usedCarLot[c] = Car('','','')
usedCarLot[c].Brand = brandList[c]
usedCarLot[c].Color = colorList[c]
usedCarLot[c].Condition = conditionList[c]

for k, v in usedCarLot.items():
print k, v.Brand, v.Color, v.Condition
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top