Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"This constructor takes no arguments" error in __init__

I'm getting an error while running the following code:

class Person:   def _init_(self, name):     self.name = name    def hello(self):     print 'Initialising the object with its name ', self.name  p = Person('Constructor') p.hello() 

The output is:

Traceback (most recent call last):     File "./class_init.py", line 11, in <module>       p = Person('Harry')   TypeError: this constructor takes no arguments 

What's the problem?

like image 647
Sagnik Kundu Avatar asked Sep 16 '12 16:09

Sagnik Kundu


People also ask

Which type of constructor takes no arguments?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new .

How do you fix a class that takes no arguments?

The Python "TypeError: Class() takes no arguments" occurs when we forget to define an __init__() method in a class but provide arguments when instantiating it. To solve the error, make sure to define the __init__() (two underscores on each side) method in the class.

What is correct syntax for defining an __ Init__ method that takes no parameters?

The “TypeError: object() takes no arguments” error is raised when you do not declare a method called __init__ in a class that accepts arguments. To solve this error, double-check your code to ensure that __init__() is spelled correctly. The method should have two underscores on either side of the word “init”.

Which is the definition of function called if it does nothing and takes no arguments in python?

The first Main.generate_noise() function definition that you give (the one without arguments) references the self variable.


1 Answers

The method should be named __init__ to be a constructor, not _init_. (Note the double underscores.)

If you use single underscores, you merely create a method named _init_, and get a default constructor, which takes no arguments.

like image 77
Sebastian Paaske Tørholm Avatar answered Oct 10 '22 06:10

Sebastian Paaske Tørholm