472,337 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

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 1337
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license"...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of...
4
by: Mike Clair | last post by:
Hey, I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I am trying to access the properties of a layer in JS which have...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or...
3
by: Michael Iantosca | last post by:
I have a custom attribute that I attach to certain pages in my application and I want to inspect each page request as it is made to see if the custom...
2
by: Markus Prediger | last post by:
Hi NG, I have an asp.net project that uses an vb6 com object for some database-manipulation (I cannot rewrite it in .net, sorry, its not my...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.