10

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.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45

4 Answers4

15

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
5

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

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
5

IMHO it doesn't make any sense at all.

  1. It makes you double check the inheritance to realize that it does nothing
  2. It's the same as adding a pass statement with the overhead of function call.
  3. Quoting the zen: Although practicality beats purity.
  4. Python 3 doesn't require you to declare object as super class.
2

Yes, and there is a reason why you should do it.

If you ever need to use multi inheritance, python's C3 method resolution order (MRO) will not call all your __init__() base classes.

Community
  • 1
  • 1
null
  • 404
  • 6
  • 12