回溯(最近一次调用最后一次):<module>中的文件“<stdin>”,第1行,TypeError:object()不带参数

时间:2018-02-11 02:53:38

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

>>> 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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

试试这个:

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()