下面是一些示例Python代碼:爲什麼不重新屬性上市的時候我在Python匹配對象上做目錄()?
import re
some_regex = re.compile(r"\s+1\s+")
result = some_regex.search(" 1 ")
dir(result)
我得到以下使用Python 2.6.1:
['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups', 'span', 'start']
然而result.re
存在(從解釋):
>>> result.re
<_sre.SRE_Pattern object at 0x10041bc90>
在使用dir()
函數時如何不能列出屬性?
本頁面確認re
屬性的存在:
http://docs.python.org/library/re.html#re.MatchObject.re
現在我明白了,如果有人試圖訪問未通過dir()
中列出的屬性,然後__getattr__
叫,但我不」 t看到__getattr__
被列爲對象的屬性之一,所以我留下了我的頭。
更新
這裏是Python 2.6.1文檔中的matchobject.re
存在的證明: http://docs.python.org/release/2.6.1/library/re.html#re.MatchObject.re
如果我在2.7.2上運行你的榜樣,DIR()做節目重新 – TJD
哦。多麼奇怪。那麼這只是2.6.1的一個錯誤? – EMiller
@emiller:不,這不是一個錯誤。它的行爲如文件記錄。 –