print hasattr(urllib2.URLError,'reason') #false
print hasattr(urllib2.URLError,'args') #true
class URLError(IOError):
# URLError is a sub-type of IOError, but it doesn't share any of
# the implementation. need to override __init__ and __str__.
# It sets self.args for compatibility with other EnvironmentError
# subclasses, but args doesn't have the typical format with errno in
# slot 0 and strerror in slot 1. This may be better than nothing.
def __init__(self, reason):
self.args = reason,
self.reason = reason
def __str__(self):
return '<urlopen error %s>' % self.reason
問題
1。 為什麼會找不到reason呢?
2。 為什麼需要用hasattr呢?直接看裡面有沒有attribute不就好了嗎?