accessing an object instance by only having one of its attribute values

mshiltonj mshiltonj at
Sun Jul 8 21:11:05 CEST 2007

On Jul 8, 2:18 pm, feli... at wrote:
> Hello all,
> Imaybe someone can help me with this question.
> Is there a direct way of accessing an object instance, if all I know
> is the value of one of its attributes?
> The object is part of a list of objects, and I would like to pick the
> object directly by using this attribute value, instead of going
> through the list and checking if each objects attribute value is the
> one I am looking for.
> Thank you in advance

I'd set up a dict as a lookup table, assuming the attribute values are
unique per object.

list_of_objects = (obj1, obj2, obj3, obj4);

obj_lookup_by_attr = {};

for obj in list_of_objects:
   obj_lookup_by_attr.set(obj.attr, obj)


obj = obj_lookup_by_attr.get(attr_val, None);

if (obj):

More information about the Python-list mailing list