避免使用 Python 的 hasattr()

不要使用 python 中的 hasattr() 除非你只在python3環境中使用它並且知道它的內部原理

總的來說,不要以如下方式使用

if hasattr(x, "y"):
    print x.y
else:
    print "no y!"

我們可以使用如下方式進行代替

try:
    print x.y
except AttributeError:
    print "no y!"

或者:

y = getattr(x, "y", None)
if y is not None:
    print y
else:
    print "no y!"

hasattr() 其實並不比 getattr() 要快, 他們使用完全相同的方式去查找屬性,只是hasattr()拋棄了結果。

爲什麼會出現這種情況:

在python 2 中, hasattr() 使用類似下面代碼的方式進行運作

try:
    print x.y
except:
    print "no y!"

這根本就不是你想要的, 他會將屬性裏面的異常全部吞掉

class C(object):
    @property
    def y(self):
        0/0
    
    def x_y(self):
        0/0

hasattr(C(), "y")
> Flase

hasattr(C(), "x_y")
> True

當你的東西使用第三方包的時候,你無法確定你使用的屬性是否是一個 property(或者在若干年之後變成一個property), 這是非常危險的

在python 3中,運作是正常的。

class C:
    @property
    def y(self):
        0/0
        
>>> hasattr(C(), "y")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in y
ZeroDivisionError: division by zero

但是你真的期望 hasattr() 去拋出一個異常嗎?

原文:https://hynek.me/articles/hasattr/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章