给定Python中的字符串列表

2024-03-29 08:53:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写了以下代码:

class CreateMap:
   def changeme(listOne, lisrTwo, listThree, listFour, listfive):

if __name__ == "__main__":
        createMap = CreateMap()
        createMap.changeme(["oneItem", "secondItem"],[],[],[],[])

它给了我以下错误:

TypeError: changeme() takes exactly 5 arguments (6 given)

据我所知,它将第一个列表识别为两个列表。我怎样才能避免呢?你知道吗


Tags: 代码name列表ifmaindefclasslistone
2条回答

它不会将第一个列表识别为两个列表。必须使用self作为函数的第一个参数,因为显式优于隐式。详细地给出了推理。我在这里引用一些。你知道吗

First, it’s more obvious that you are using a method or instance attribute instead of a local variable. Reading self.x or self.meth() makes it absolutely clear that an instance variable or method is used even if you don’t know the class definition by heart.

将您的功能定义为

def changeme(self,listOne, lisrTwo, listThree, listFour, listfive):

这将使类外的实例变量可以访问函数

相关问题 更多 >