所以在python的docutils包中有一個類(Image),它有一個方法(align)。據我所知,它的方法以自身作爲第一個參數,除非它們被裝飾爲@classmethod或@staticmethod,但是align不會。相關代碼複製如下(full code here)。python類中的實例方法和靜態方法
class Image(Directive):
def align(argument):
# This is not callable as self.align. We cannot make it a
# staticmethod because we're saving an unbound method in
# option_spec below.
return directives.choice(argument, Image.align_values)
我使用這個代碼,作爲我自己的目的的基地,我曾經嘗試都給人對齊自我論證,並把它變成一個靜態方法(更改名稱,以免與自己發生衝突後, .align),但兩種方法都有錯誤。到底是怎麼回事?
你能提供什麼錯誤嗎? – MaLiN2223
with @staticmethod:'staticmethod'對象不可調用。添加'self':align_option()缺少1個必需的位置參數:'arg'。 – vorpal
實際上這是記錄:「這不能作爲self.align調用,我們不能將它作爲靜態方法,因爲我們在下面的option_spec中保存了一個未綁定的方法。」 –