473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1381
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" for more information. >>> o = object() >>> o.a...
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 suggestions, I'm thinking more and more that instead 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 been initially set in an external CSS. The problem...
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 method" when I execute the following code. What I...
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 attribute is attached to the underlying page...
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 decision). I want it to be instanciated seperately...
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 is problem that I'd like to discuss with you...
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 some select fields in a form I created for a...
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 properties like MyObject.MyProperty MyObject...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.