473,398 Members | 2,088 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,398 software developers and data experts.

Variable interpolation question

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
Jul 18 '05 #1
3 1599
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.

Jul 18 '05 #2
On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <an********************************@rambler.ru> 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
Jul 18 '05 #3
Bengt Richter wrote:
On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <an********************************@rambler.ru> 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.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Horst | last post by:
I have a tiny custom 404 script I'm working on; I'd like to imitate the standard Apache 404 page insofar as I'd like "Apache/1.3.29 Server at www.example.com Port 80" to appear below my 404...
3
by: red | last post by:
within an object function, this code: $test2='test'; echo "$test"; $this->test2='test2'; echo "$this->test2\n"; produces this result: test2
7
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump...
14
by: Charles Banas | last post by:
I'm not sure if this is the right place to ask about this, but I've seen several posts in the past regarding Akima's Bivariate Interpolations routines, and i'm wondering if someone can give me some...
3
by: Jonas Ernst | last post by:
Hi, Can somebody give me some hints how to do a line interpolation without using floating point arithemtics? The function shall do a linear interpolation between 2 points (line interp?) and...
4
by: Jason Dunbar | last post by:
Hi there.. I am wondering if it is possible to interpolate the value of a variable to reference a variable to gain its value. For example:- if I have a series of variables:- var01
8
by: Jerry | last post by:
I am a MySQL and PHP newbie. I am having trouble getting the $w variable in my code below passed to mysql. When I use the value of $w directly in the Where clause, the correct rows are returned....
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
5
by: August Karlstrom | last post by:
Hi, Does anyone know the correct syntax to interpolate a class variable, $x say, inside a string? I tried "{self::$x}" but it produces the string {self::x}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.