Variable interpolation question

A

Andrew Fabbro

This is probably a beginner's question, but I'm stuck...please be kind
to an ex-perler ;)

How do I do something like this:

for attr in dir(some_obj):
if ( some_obj.attr == 0 ):
print "Missing data: %s field %s" % ( some_obj.name,
some_obj.attr)

Of course, this gives
"AttributeError: foo instance has no attribute 'attr'"

I really don't want to use exec/eval, as that slows things down
dramatically.

Help?

Thanks.

-Drew
 
A

anton muhin

Andrew said:
This is probably a beginner's question, but I'm stuck...please be kind
to an ex-perler ;)

How do I do something like this:

for attr in dir(some_obj):
if ( some_obj.attr == 0 ):
print "Missing data: %s field %s" % ( some_obj.name,
some_obj.attr)

Of course, this gives
"AttributeError: foo instance has no attribute 'attr'"

I really don't want to use exec/eval, as that slows things down
dramatically.

Help?

Thanks.

-Drew

You are probably looking for hasattr/getattr functions:

for attr in dir(some_obj):
if hasattr(some_obj, attr) and getattr(some_obj, attr) == 0:
print 'blah...'

of course, it could be shorter:

.....
if getattr(some_obj, attr, 0) == 0:
print 'blah'

regards,
anton.
 
B

Bengt Richter

You are probably looking for hasattr/getattr functions:

for attr in dir(some_obj):
if hasattr(some_obj, attr) and getattr(some_obj, attr) == 0:
print 'blah...'

of course, it could be shorter:

....
if getattr(some_obj, attr, 0) == 0:
print 'blah'
Or why use an "== 0" test when just hasattr tests for presence/absence without using up
a possible value of the attribute for flag purposes?

BTW, The OP might want to note that in general

for attr in dir(some_object):
if getattr(some_obj, attr, 0) == 0:
...

is not the same as (untested)

objdict = vars(some_obj) # vars raises exception if there's no some_obj.__dict__
for attr in objdict:
if objdict[attr] == 0:
...

or, safer, (untested)

objdict = getattr(some_object, '__dict__', {})
for attr in objdict:
if objdict[attr] == 0:
...

or (untested)

for attr in getattr(some_object, '__dict__', {}): ...
if some_obj.__dict__[attr] == 0: ...
...

I.e., dir chases down all the inherited stuff, and getattr invokes all the magic
associated with attribute access, such as properties, whereas just using
some_obj.__dict__ bypasses that.

Regards,
Bengt Richter
 
A

anton muhin

Bengt said:
Andrew Fabbro wrote:



You are probably looking for hasattr/getattr functions:

for attr in dir(some_obj):
if hasattr(some_obj, attr) and getattr(some_obj, attr) == 0:
print 'blah...'

of course, it could be shorter:

....
if getattr(some_obj, attr, 0) == 0:
print 'blah'

Or why use an "== 0" test when just hasattr tests for presence/absence without using up
a possible value of the attribute for flag purposes?
[skipped]
Regards,
Bengt Richter

Of course you're right, I just forgot it and thought that OP wants to
verify attribute's presence as well---mea culpa.

regards,
anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top