缺少 1 个必需的位置参数:“名称”

社会演员多 python 200

原文标题missing 1 required positional argument: ‘name’

为什么 init() 出现错误缺少 1 个必需的位置参数:’name’

class DOM:

    def __init__(self,name):
        self.name = name
    def __iadd__(self, other): 
        self.name+=other.name

原文链接:https://stackoverflow.com//questions/71919740/missing-1-required-positional-argument-name

回复

我来回复
  • Ishan Shishodiya的头像
    Ishan Shishodiya 评论

    您收到错误是因为您在创建类对象时没有传递name参数。

    class DOM:
      def __init__(self,name):
        self.name = name
      def __iadd__(self, other): 
        self.name+=other.name
    
    dom = DOM()
    

    给-

    TypeError: __init__() missing 1 required positional argument: 'name'
    

    要解决此问题,请传入 name 参数的值

    dom = DOM(name = "any value of your choice")
    
    2年前 0条评论