Python Forum
class and runtime - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: class and runtime (/thread-41759.html)



class and runtime - akbarza - Mar-15-2024

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


RE: class and runtime - deanhystad - Mar-15-2024

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.


RE: class and runtime - DeaD_EyE - Mar-15-2024

(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.


RE: class and runtime - akbarza - Mar-16-2024

hi
thanks for reply
where can i find an example that showing modifying a class at run time?
thanks again


RE: class and runtime - deanhystad - Mar-16-2024

google python monkey patching. Plenty of examples.