Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Variable interpolation question

Reply
Thread Tools

Variable interpolation question

 
 
Andrew Fabbro
Guest
Posts: n/a
 
      11-17-2003
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
 
Reply With Quote
 
 
 
 
anton muhin
Guest
Posts: n/a
 
      11-17-2003
Andrew Fabbro wrote:

> 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.

 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      11-17-2003
On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <(E-Mail Removed)> wrote:

>Andrew Fabbro wrote:
>
>> 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'
>

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
 
Reply With Quote
 
anton muhin
Guest
Posts: n/a
 
      11-17-2003
Bengt Richter wrote:
> On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <(E-Mail Removed)> wrote:
>
>
>>Andrew Fabbro wrote:
>>
>>
>>>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'
>>

>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
double variable interpolation question ccc31807 Perl Misc 8 05-22-2009 08:13 PM
heredoc with variable interpolation breaks script Oliver Heins Ruby 2 09-11-2006 03:59 PM
How to stop interpolation of Escape character in variable? \Rob\ ASP .Net 8 02-24-2006 08:52 AM
Variable Interpolation with %%variable phrankster Perl Misc 9 08-14-2005 12:19 AM
how to resolve a variable but disallow interpolation in regex match Praveen Kallakuri Perl Misc 3 04-15-2004 02:29 AM



Advertisments