[Tutor] is it possible to create and amend classes during run-time?
alan.gauld at btinternet.com
Thu May 3 01:01:27 CEST 2012
>> Does anyone know if it is possible during run-time to:
>> a) add attributes to classes, which will unknown at program start, but
>> "emerge" later whilst the program is running?
Yes, but its not very useful since the rest of your code won't know how
to access those attributes. There are ways around that (getattr() etc)
but in general it's a bad design pattern that should be avoided.
But Python is an interpreted language so anything you can do in a script
file can also be done at runtime.
>> b) add subclasses to class (also during runtime)
I'm not sure what you mean, but I'm guessing you are asking if you can
subclass an existing class. That is create a new class, and again the
answer is yes.
Author of the Learn to Program web site
More information about the Tutor