[Python-Dev] New syntax for 'dynamic' attribute access
Giovanni Bajo
rasky at develer.com
Tue Feb 13 11:46:28 CET 2007
On 13/02/2007 7.39, Martin v. Löwis wrote:
> And again. Apparently, people favor hasattr over catching
> AttributeError. I'm not sure why this is -
Because the code becomes longer, unless you want to mask other exceptions:
name = 'http_error_%d' % errcode
-if hasattr(self, name):
- method = self.(name)
+try:
+ method = self.(name)
+except AttributeError:
+ pass
+else:
if data is None:
result = method(url, fp, errcode, errmsg, headers)
else:
result = method(url, fp, errcode, errmsg, headers, data)
if result: return result
return self.http_error_default(url, fp, errcode, errmsg, headers)
--
Giovanni Bajo
More information about the Python-Dev
mailing list