Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class and runtime
#1
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
Reply
#2
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.
akbarza likes this post
Reply
#3
(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.
akbarza likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
hi
thanks for reply
where can i find an example that showing modifying a class at run time?
thanks again
Reply
#5
google python monkey patching. Plenty of examples.
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  painfully slow runtime when handling data dadazhu 3 976 Jan-20-2023, 07:11 PM
Last Post: snippsat
  Big O runtime nested for loop and append yarinsh 4 1,396 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,227 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Object reference in Dict - not resolved at runtime benthomson 2 1,849 Apr-02-2020, 08:50 AM
Last Post: benthomson
  PyCharm asking for VC++ runtime 14.0 whereas I have already installed VC++ 19.0 SarmadiRizvi 1 1,833 Apr-02-2020, 06:17 AM
Last Post: snippsat
  EOFError: EOF when reading a line - Runtime Error RavCOder 6 9,690 Sep-27-2019, 12:22 PM
Last Post: RavCOder
  Different runtime programs mhvozdesky 1 44,734 Oct-24-2018, 02:50 PM
Last Post: ichabod801
  PyDev package install/download at runtime user2103 0 2,485 Jan-30-2018, 08:39 AM
Last Post: user2103
  Question on runtime and improving nested for loops ackmondual 1 3,070 Jun-13-2017, 11:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020