By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

style question - hasattr

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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