【问题标题】:Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object() takes no parameters回溯(最后一次调用):文件“<stdin>”,第 1 行,在 <module> 类型错误:object() 不带参数
【发布时间】:2018-02-11 02:53:37
【问题描述】:
>>> class student:
    def _init_(self,name,age):
        self.name
        self.age
    def display(self):
        return("this is a "+self.name+str(self.age))
>>> stu=student("chad",14)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters

我想知道我哪里出错了,我该如何解决这个问题。

【问题讨论】:

    标签: python python-3.x python-2.7 subprocess


    【解决方案1】:

    __init__() 是个笨蛋。它以__ 开头和结尾,双下划线,又名:dunder。将_init_ 更改为__init__

    代码:

    class student:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def display(self):
            return ("this is a " + self.name + str(self.age))
    
    stu = student("chad", 14)
    print(stu.display())
    

    结果:

    this is a chad14
    

    【讨论】:

      【解决方案2】:

      试试这个:

      class student:
        def __init__(self,name,age):
          self.name = name
          self.age = age  
      
        def display(self):
          stu=student("chad",14)
          print("this is a "+(stu.name)+str(stu.age))
      
      s = student(None,None)
      s.display()
      

      【讨论】:

      • 0 不赞成票 >>> 班级学生: def _init_(self,name,age): self.name self.age def display(self): return("this is a "+self. name+str(self.age)) >>> stu=student("chad",14) Traceback(最近一次调用最后一次):文件“”,第 1 行,在 类型错误:object() 需要无参数
      • 我在我的程序中犯了一个错误..因为我想知道返回类型
      • "init" 方法两边应该有两个“_”。
      • 并且在 inti 方法中,您应该将这些参数变量分配给您的类变量。如果不是,它就不起作用,因为没有分配给他们返回。如果要打印返回的内容,则返回时必须使用 print 方法。试试这个 -> class student: def __init__(self,name,age): self.name = name self.age = age def display(self) : return("这是一个"+self.name+str(self.age)) stu = student('cha',14) print(stu.display())
      • import re >>> import urllib.request >>> url="http:google.com" >>> stock=input("enter your search :") enter your search :Fb >> > url=url+stock >>> 数据=urllib.request.urlopen(url).read()
      猜你喜欢
      • 2019-04-01
      • 2022-11-27
      • 2022-10-13
      • 2019-02-25
      • 1970-01-01
      • 2022-01-10
      • 2023-03-11
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多