noob help request - how to make a list of defined class?

nuffnough at nuffnough at
Tue Sep 9 16:11:01 CEST 2008

I have defined two classes with one common field (called code) and
several different fields.
In class A there is only one instance of any given code as all items
are individual.
In class B, there may be none, one or many instances of each code, as
there can be any number of Bs referring to a single A.

I need to make a list of Bs, remove dupes, and then create a list of
As that have Bs.

(I hope this makes sense!)

with much research and reading of my book I managed to get this
working,  but at one stage I had errors that talked about being unable
to concatenate type A.  So I converted my As to string, and then did a
split on commas to make a list.  This worked fine for all the As that
didn't have a comma in a field (about 85%) but I can't help feeling
that this is a kludge, and that if Icould find a way to get a proper
list of As instead of a list of lists created by converting to string
and splitting, then my app would work properly.

So I am hoping some of the nice folk here will help me out.

The relevent function looks like this:

def gen_B_A_list():
    Bcodes = get_codes()
    all_As = read_A("A.csv")
    B_A = []
    for i in range(len(codes)):
        Bcode = Bcodes[i]
        for A in all_As:
            filename = getattr(A, 'code')
            if filename == Bcode:
                BA = str(A)
                BA = string.split(BA, ',')
    return B_A

The element in question is after if filename == Bcode.   How do I
construct a list of my defined class A objects here instead of this?
( I can post the full code if needed,  just thought it was better
netiquette not to)



More information about the Python-list mailing list