468,140 Members | 1,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

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

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

Jul 8 '07 #1
5 1215
fe*****@gmail.com 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
Okay, imagine you got a bunch of small packets (presents or something
similiar). You want to get rid of the lightest one. You cannot see
weights, so, what you're doing is basically measuring the weight of
*every single packet*, compare and pick. No way around.

There might be some ways not having to touch *each* object, if you
precompute some results. So, in the example above, you could divide the
packets into two categories, "more than 50 lbs" and "less than 50 lbs",
for example. I think binary trees might be interesting here but touching
every object would be way easier.

HTH,
Stargaming
Jul 8 '07 #2
On Jul 8, 2:18 pm, feli...@gmail.com 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):
[...]

Jul 8 '07 #3
On Jul 8, 2:11 pm, mshiltonj <mshilt...@gmail.comwrote:
On Jul 8, 2:18 pm, feli...@gmail.com 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):
[...]
I have some comments on the Pythonicity of your suggestions. Same
assumption, object attr is a unique key of some sort. How to create
the dict of objects, and how to retrieve an object by key.

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

# creating a dict by initializing to empty, and then looping
# through input list and adding items one at a time
obj_lookup_by_attr = {};
for obj in list_of_objects:
#obj_lookup_by_attr.set(obj.attr, obj)
# why use set for this? why not just this:
obj_lookup_by_attr[obj.attr] = obj

# or even better might be to use a generator expression to create a
# sequence of tuples, and call the dict constructor - a good idiom to
# learn
obj_lookup_by_attr = dict( (obj.attr,obj) for obj in list_of_objects )

[...]

obj = obj_lookup_by_attr.get(attr_val, None);

# oh woe if obj is an instance of a class that defines special
# true/falseness - obj might be a real object, but evaluates to false
#
#if (obj):
#
# much better to test this way
# (and NEVER test using "if obj != None:" - this is wasteful
# nonsense, since None is a singleton)
if obj is not None:
[...]

Jul 9 '07 #4
On Jul 8, 8:29 pm, Paul McGuire <p...@austin.rr.comwrote:
On Jul 8, 2:11 pm, mshiltonj <mshilt...@gmail.comwrote:
I have some comments on the Pythonicity of your suggestions. Same
assumption, object attr is a unique key of some sort. How to create
the dict of objects, and how to retrieve an object by key.

-- Paul
I was probably being overly helpful, in a bad way. I'm new to python,
I'm not very pythonic yet, and still learning the python idioms.

Not sure why I slipped into the habit of testing for None, though. :-(

Probably a perl thing, where I'm regularly testing for defined-ness.
"if ($foo)" is different than "if (defined $foo)"

Still, I'm really like python so far.

Jul 9 '07 #5
mshiltonj a écrit :
On Jul 8, 8:29 pm, Paul McGuire <p...@austin.rr.comwrote:
>On Jul 8, 2:11 pm, mshiltonj <mshilt...@gmail.comwrote:
>I have some comments on the Pythonicity of your suggestions. Same
assumption, object attr is a unique key of some sort. How to create
the dict of objects, and how to retrieve an object by key.

-- Paul

I was probably being overly helpful, in a bad way. I'm new to python,
I'm not very pythonic yet, and still learning the python idioms.

Not sure why I slipped into the habit of testing for None, though. :-(

Probably a perl thing, where I'm regularly testing for defined-ness.
"if ($foo)" is different than "if (defined $foo)"
I'm not sure about the exact meaning of "defined-ness" in Perl, but in
Python, trying to access a non-existing name - which is not the same
thing as a name bound to None - will raise a NameError.

And while we're at it, you definitively don't need the parens around the
test.

Jul 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Krzysztof Stachlewski | last post: by
4 posts views Thread by Mike Clair | last post: by
3 posts views Thread by Michael Iantosca | last post: by
2 posts views Thread by Markus Prediger | last post: by
7 posts views Thread by Chuck Anderson | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.