简体   繁体   中英

Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object() takes no parameters

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

I want to know where I went wrong and how can I resolve this.

__init__() is a dunder. It starts and ends with __ , a double underbar, aka: dunder. Change _init_ to __init__ .

Code:

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

Results:

this is a chad14

Try this:

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

Just put a new line after the class definition.
Also, I solved some errors in your code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM