Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call super().__init__() in classes derived from `object`?

Tags:

python

The Python documentation says that the __init__ method of each class is responsible for initializing its super class. But for new-style classes, the ultimate base class is object. Doing dir(object) shows that object itself has an __init__ method and could potentially be initialized. Is there any reason to do that?

I'm inclined to do it for consistency and (slightly) easier refactoring of the class heirarchy, but I wonder if it's strictly necessary or is considered best practice.

like image 988
Codie CodeMonkey Avatar asked Jul 22 '11 23:07

Codie CodeMonkey


2 Answers

You don't need to initialize object; its __init__ is a no-op. It's still good practice, though, as you might want to introduce an intermediate class in the hierarchy later on.

like image 113
Fred Foo Avatar answered Nov 06 '22 04:11

Fred Foo


Yes, do it. It's a good habit to get into, and it doesn't hurt.

like image 26
Ned Batchelder Avatar answered Nov 06 '22 06:11

Ned Batchelder