On May 2, 2006, at 8:42 PM, Daniel Miller wrote:


Oh, and use a try/except block rather than hasattr. It's more pythonic... So with all those suggestions this is what we end up with:


def default_session(obj=None):
   try:
       return obj.__default_session__()
   except AttributeError:
       return _default_session(obj=obj)


if try/except were more "pythonic" than "hasattr", then python shouldnt have an "hasattr" function. The try/except pattern is more appropriate when the try: block will be called in almost all cases and almost never the except: block, such as when the missing attribute is initialized in the except: block so that it is only called once. when the missing attribute is the norm, as it is in this case since objects will only have a __session__ function in the case of a certain configuration, the except: block adds a lot more overhead than hasattr.

apparently, this is one of the oldest debates in the book:

        http://mail.python.org/pipermail/python-list/2000-January/021450.html

but this doc should say it all:

        http://docs.python.org/lib/node465.html

this example shows hasattr as about 3.6 times faster than try/except for a missing attribute, whereas try/except is only 1.5 times faster than hasattr for a present attribute (i tried it locally with py2.4 and got the same results).




-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to