When I import a module that has a class, what code is executed when that class is first read and the class object created? Is there any way I can influence what happens?
Edit: I realize my question might be a bit too general…
I’m looking for something more low-level which will allow me to do introspection from C++. I extend my C++ application with Python. I have some classes that are defined in C++ and exposed in Python. The user can inherit from these classes in the scripts and I want to be able to grab details about them when they are first defined.
Many possible things can happen. The most basic:
The contents of the
classblock are executed when the it is first read.To see this in action, this example:
Will print
barwhen the module is imported.If a class has a metaclass defined, the metaclasses
__new__function will run after the classes block of code is run.Example:
Output:
I’m sure other bits can run as well, these are just what I am familiar with.