【问题标题】:takes 1 positional argument but 2 were given error for dict接受 1 个位置参数,但 2 被 dict 错误
【发布时间】:2023-04-01 10:16:02
【问题描述】:

如何在cs列表中添加两个dict

我想在addcoursess函数的cs列表中添加两个字典,但是报错 文本错误:接受 1 个位置参数,但给出了 2 个

主列表:

    cs = [
    {
        "title": "Python",
        "teacher": "Amiri",
    },
    {
        "title": "HTML",
        "teacher": "Dehyami",
    },
    {
        "title": "PHP",
        "teacher": "Enayati"
    }
]


class User:
    def __init__(self, fisrtname, lastname):
        self.fname = fisrtname
        self.lname = lastname

    def fullname(self):
        print(f"my fullname is {self.fname} {self.lname}")

class Student(User):
    def __init__(self, fisrtname, lastname, email):
        super().__init__(fisrtname, lastname)
        self.email = email
        self.coursess = []

    def fullname(self):
        print("i am an student")
        super().fullname()

    def printcoursess(self):
        if self.coursess:
            for course in self.coursess:
                print("Coursess : " + course["title"])
        else:
            print("There is no course")

这是错误所在的类

class Teacher(User):
    def __init__(self, fisrtname, lastname, code):
        super().__init__(fisrtname, lastname)
        self.code = code

    def addcoursess(item):
        dict = {}
        dict.update(item)
        cs.append(dict)
        print(dict)
    def fullname(self):
        print("i am an teacher")
        super().fullname()


t1 = Teacher("abolfazl", "zaker", 3223)

在这里添加课程功能

t1.addcoursess({"title": "Java", "teacher": "ganjeali"})

print(cs)

【问题讨论】:

  • 与问题无关,但很重要:不要调用自己的变量dict,因为它会影响同名的内置变量。

标签: python django class dictionary oop


【解决方案1】:

你错过了函数定义中的self 参数。您的教师课程应如下所示:

class Teacher(User):
    def __init__(self, fisrtname, lastname, code):
        super().__init__(fisrtname, lastname)
        self.code = code

    def addcoursess(self, item):
        dict = {}
        dict.update(item)
        cs.append(dict)
        print(dict)

    def fullname(self):
        print("i am an teacher")
        super().fullname()

或者如果您不想传递self,则应使用@staticmethod 装饰器定义该类,如下所示:

class Teacher(User):
        def __init__(self, fisrtname, lastname, code):
            super().__init__(fisrtname, lastname)
            self.code = code

        @staticmethod
        def addcoursess(item):
            dict = {}
            dict.update(item)
            cs.append(dict)
            print(dict)

        def fullname(self):
            print("i am an teacher")
            super().fullname()

关于静态方法的信息在这里:https://docs.python.org/3/library/functions.html#staticmethod

【讨论】:

    【解决方案2】:

    您的教师课堂方法缺少 self,关键字。它应该如下所示

    class Teacher(User):
    def __init__(self, fisrtname, lastname, code):
        super().__init__(fisrtname, lastname)
        self.code = code
    
    def addcoursess(self, item):
        dict = {}
        dict.update(item)
        cs.append(dict)
        print(dict)
    
    def fullname(self):
        print("i am an teacher")
        super().fullname()
    

    【讨论】:

      【解决方案3】:

      您忘记在Teacher 类的addcoursess 方法中添加self,它应该是addcourse(self,item):

      这是您的完整工作代码。

      cs = [
          {
              "title": "Python",
              "teacher": "Amiri",
          },
          {
              "title": "HTML",
              "teacher": "Dehyami",
          },
          {
              "title": "PHP",
              "teacher": "Enayati"
          }
      ]
      
      
      class User:
          def __init__(self, fisrtname, lastname):
              self.fname = fisrtname
              self.lname = lastname
      
          def fullname(self):
              print(f"my fullname is {self.fname} {self.lname}")
      
      class Student(User):
          def __init__(self, fisrtname, lastname, email):
              super().__init__(fisrtname, lastname)
              self.email = email
              self.coursess = []
      
          def fullname(self):
              print("i am an student")
              super().fullname()
      
          def printcoursess(self):
              if self.coursess:
                  for course in self.coursess:
                      print("Coursess : " + course["title"])
              else:
                  print("There is no course")
      
      class Teacher(User):
          def __init__(self, fisrtname, lastname, code):
              super().__init__(fisrtname, lastname)
              self.code = code
      
          def addcoursess(self, item):
              dict = {}
              dict.update(item)
              cs.append(dict)
              print(dict)
          def fullname(self):
              print("i am an teacher")
              super().fullname()
      
      
      t1 = Teacher("abolfazl", "zaker", 3223)
      
      t1.addcoursess({"title": "Java", "teacher": "ganjeali"})
      
      print(cs)
      

      【讨论】:

        【解决方案4】:

        你忘记了声明中的自我

         def addcoursess(self, item):
        

        我的结果如下:

        [{'teacher': 'Amiri', 'title': 'Python'}, {'teacher': 'Dehyami', 'title': 'HTML'}, {'teacher': 'Enayati', 'title': 'PHP'}, {'teacher': 'ganjeali', 'title': 'Java'}]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-15
          • 2021-08-18
          • 2021-12-30
          • 2020-03-08
          • 1970-01-01
          • 2020-12-29
          • 2016-10-13
          • 1970-01-01
          相关资源
          最近更新 更多