In everyday Python coding, especially for new Python programmers, we often encounter errors like this:

TypeError: object() takes no parametersCopy the code

It’s easy to confuse us because the error message doesn’t clearly indicate which code is responsible for the problem. Then how did this mistake come about? Please listen to me carefully.

In Python, a method is a property. That is, when we call a method, Python needs the property that corresponds to the name of the method, for example:

o.m()Copy the code

Python will search for m attributes on object O and call it if object O has m attributes.

However, Python methods are defined in a class, not object. That is, if M is a method of O, it can’t be a property of it. Normally, Python searches for the attributes of the object first, and if not, searches for the attributes of the class. If the attributes exist, it can be called. A class is an instance of an object, and an object is an instance of an object.

In Python, most classes inherit from object. In Python3, if you do not specify an object inheritance, the interpreter automatically adds it to you, whereas in Python, if you do not specify an old-style class, it is an old-style class. When we usually write classes, it is recommended that we add inheritance object, such a code compatibility number, one is more elegant.

If the property does not exist in the object, we will get an error message indicating where the code is broken and why, but the same as the error we mentioned above

TypeError: object() takes no parametersCopy the code

This error is reported when I create an object instance, for example:

class Foo(object):
    passCopy the code

If I do this:

f = Foo()Copy the code

There won’t be any problems, but if I do this:

f = Foo(10)Copy the code

Then I get the error above, why is that?

This is because Python creates objects in two stages: in the first stage, objects are created by calling the new method, the details of which we don’t care much about. The new method does not immediately return an object instance. After the new method, init is called to add new properties to the object. For the above object O, the call is

o.__init__()Copy the code

Python first looks for the init method of o, but it doesn’t find it, and then looks for the init method of the parent class, assuming that the parent class is Foo above, and the init method still doesn’t exist, so it finally finds the init property of object. Object init exists, and it is a method, and we call this method, passing in the appropriate parameters, but object.init has no parameters, and we get the above error.

TypeError: object() takes no parametersCopy the code

The most confusing part of the process is that Python does not report an error like this:

"Object. The __init__ ()" takes no parametersCopy the code

So we don’t know exactly what the problem is.

In summary, when implementing a Python class, write the init method last to avoid this deceptive error.

Reprint my blogThe snake said