簡體   English   中英

返回元組給錯誤的元組對象是不可調用的

[英]Returing tuple giving an error tuple object is not callable

class Foo:
    def __init__(self):
        pass

    @property
    def get_features(self):
        return (1,2,3,)

    def execute(self):
        print self.get_features()

f = Foo()
f.execute()

我得到:

TypeError: 'tuple' object is not callable

我感興趣的實際上是該元組的長度。

您不應該調用屬性。 相反,您可以像普通屬性一樣訪問它們:

def execute(self):
    print self.get_features

屬性和屬性之間的唯一區別是屬性具有getter和setter函數,當您訪問或設置它們的值時它們會隱式調用。 有關更多信息,請參見property文檔

另外,您的類應該繼承自object

class Foo(object):

您應該始終在Python 2.x中執行此操作,以使您的類成為新樣式的類 ,其功能遠遠超過舊樣式的類。

原因是您在get_features()函數上具有@property裝飾器,使其成為屬性,因此無法調用。

這意味着execute()只需打印屬性,而不是將其作為函數調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM