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?
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 .
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.
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”.
The first Main.generate_noise() function definition that you give (the one without arguments) references the self variable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With