Python Forum

Full Version: class and runtime
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
in address:https://docs.python.org/3/tutorial/classes.html
is written:
Quote:As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.
can explain this statement? what is the importance of the creation of a class in runtime? how can a class be modified after creation?
thanks
You can add methods to a class or replace existing methods with different code during runtime. I embed an interactive python interpreter in some of the systems I make. During debugging and checkout I can open a console to the interpreter a type python code. Sometimes I modify the code for a class so a method acts differently when called.

Modifying a class after it is included is rare, but common enough that there is a term for it: monkey patching.
(Mar-15-2024, 08:02 AM)akbarza Wrote: [ -> ]can explain this statement? what is the importance of the creation of a class in runtime? how can a class be modified after creation?

This is not possible with static compiled languages. During the compilation, the compiler needs to know exactly all data types.
A dynamic language allows creation of objects during runtime. In Python, classes are also objects.

This allows to create or change behavior of classes (types), but not everything which is possible, is done.
hi
thanks for reply
where can i find an example that showing modifying a class at run time?
thanks again
google python monkey patching. Plenty of examples.