469,293 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

style question - hasattr

ian
In old python code i would use 'has_key' to determine if an element
was present in a dictionary.

Python 3.0 will even removed 'has_key'. The reason for removal is that
using the 'in' operator is a cleaner syntax and having two ways to
achieve the same result is against the principle of the language.

Ok, so what about 'hasattr' ??
hasattr(myObject,'property')
seems equivalent to
'property' in dir(myObject)

I would suggest that using the 'in' is cleaner in this case also. Is
there a performance penalty here? Or is there reason why the two are
not actually the same?

Which style is preferred??
Apr 9 '08 #1
1 1688

"ian" <ia*************@saltmob.comwrote in message
news:a5**********************************@w8g2000p rd.googlegroups.com...
| In old python code i would use 'has_key' to determine if an element
| was present in a dictionary.
|
| Python 3.0 will even removed 'has_key'. The reason for removal is that
| using the 'in' operator is a cleaner syntax and having two ways to
| achieve the same result is against the principle of the language.
|
| Ok, so what about 'hasattr' ??
| hasattr(myObject,'property')
| seems equivalent to
| 'property' in dir(myObject)
|
| I would suggest that using the 'in' is cleaner in this case also. Is
| there a performance penalty here?

Yes, the construction of the dir.
And I think there may be slight differences between the two.

tjr

Apr 9 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nicholas Wieland | last post: by
2 posts views Thread by Dragos Chirila | last post: by
16 posts views Thread by Chris | last post: by
2 posts views Thread by Brian Roberts | last post: by
18 posts views Thread by marduk | last post: by
13 posts views Thread by Thomas Heller | last post: by
86 posts views Thread by PTY | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.