O'Reilly logo

Python: Journey from Novice to Expert by Rick van Hattem, Dusty Phillips, Fabrizio Romano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Order of operations when instantiating classes

The order of operations during class instantiation is very important to keep in mind when debugging issues with dynamically created and/or modified classes. The instantiation of a class happens in the following order.

Finding the metaclass

The metaclass comes from either the explicitly given metaclass on the class or bases, or by using the default type metaclass.

For every class, the class itself and the bases, the first matching of the following will be used:

  • Explicitly given metaclass
  • Explicit metaclass from bases
  • type()

    Note

    Note that if no metaclass is found that is a subtype of all the candidate metaclasses, a TypeError will be raised. This scenario is not that likely to occur but certainly a possibility ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required