[Tutor] Deriving from base class, adding an attribute

Jeff Kowalczyk jtk@yahoo.com
Tue, 30 Jul 2002 12:50:03 -0400


Pardon the ultra-newbie question, but I want to derive a number of
classes (in a single module) from the classes in another module.
To each of the classes, I need to add a single attribute (if in fact
attributes are what they're properly called):

The base product is Formulator, the module StandardFields.py
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/formulator/Formulator/

Products\Formulator\StandardFields.py (original)
-----------------------------------------------
class StringField(ZMIField):
    meta_type = "StringField"
    widget = Widget.TextWidgetInstance
    validator = Validator.StringValidatorInstance

class PasswordField(ZMIField):
    meta_type = "PasswordField"
    widget = Widget.PasswordWidgetInstance
    validator = Validator.StringValidatorInstance
(...)

What I need to add to my derived module (pseudocode):
Products\MyFormulator\StandardFields.py
-----------------------------------------------
from Layout import Layout
import Products.Formulator.StandardFields

class StringField(Formulator.StandardFields.StringField):
 layout = Layout.LayoutInstance

class PasswordField(Formulator.StandardFields.StringField):
    layout = Layout.LayoutInstance
(...)

Where Layout.py is a module in this derived Python product. How
is this intent supposed to be expressed?

I would like to do this the manual way first, so I learn about
module imports and python's inheritance syntax. But once I
understand that, I would imagine that Python's nifty introspection
features could make something like this possible:
"For all classes deriving from X in module Y, add attribute Z"