[Python-Dev] getattr vs hashattr (original) (raw)

Brett Cannon brett at python.org
Mon Mar 17 15:05:50 CET 2014


On Fri, Mar 14, 2014 at 3:01 PM, Chris Withers <chris at simplistix.co.uk>wrote:

On 03/12/2014 04:49 PM, Chris Angelico wrote:

You can use hasattr() in place of AttributeError

Is that true now? It used to be that hasattr swallowed all exceptions rather than just AttributeError making is a very dangerous weapon for anything (such as an orm or odb) that might do something interesting when you try and get an attribute from it.

Changed in Python 3.2: http://docs.python.org/3.2/whatsnew/3.2.html

On 12/03/2014 21:37, Tres Seaver wrote:

I use:

getattr(subject, attrname, default)? all the time. On this one, did anything ever come of making getattr have a default default of None?

No. If you want None returned as the default you still need to specify that to allow for those APIs where the attribute can have a value of None but you still want it to raise an AttributeError if the attribute isn't there.

-Brett

Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ brett%40python.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20140317/aaa4bd6f/attachment.html>



More information about the Python-Dev mailing list