user1530405
user1530405

Reputation: 455

List.append error

I get an error on line 17 "Comps.append(Props(look))".

I am trying to search a List of "Records" for the existence of a certain item, and if it is not in the list append it to the end.

Can anybody help ?

class Props(object):
    def __init__(self, Name = None):
        self.Name = Name

a = '111'
Comps = []
Comps.append(Props('aaa'))
Comps.append(Props('bbb'))
Comps.append(Props(a))

look = 'ccc'
for Props in Comps:
    if look in Props.Name:
        print 'Found Duplicate - ', look
        break
    else:
        Comps.append(Props(look))        # TypeError: 'Props' object is not callable

for Props in Comps:
    print (Props.Name)

Upvotes: 1

Views: 275

Answers (1)

Mogsdad
Mogsdad

Reputation: 45710

You have overloaded the meaning of Props before line 17, with this:

for Props in Comps:

Since Props is a class, you should not use it as an iterator as well. Instead:

class Props(object):
    def __init__(self, Name = None):
        self.Name = Name

a = '111'
Comps = []
Comps.append(Props('aaa'))
Comps.append(Props('bbb'))
Comps.append(Props(a))

look = 'ccc'
for el in Comps:
    if look in el.Name:
        print 'Found Duplicate - ', look
        break
    else:
        Comps.append(Props(look)) 

for el in Comps:
    print (el.Name)

You can also simplify the whole search / append operation as well: Thanks Jon!

if not any(el.Name == look for el in Comps): Comps.append(Props(look))

Upvotes: 1

Related Questions