Class introspection and dynamically determining function arguments

Mike C. Fletcher mcfletch at rogers.com
Sat Jan 22 02:23:58 CET 2005


>On Thu, 20 Jan 2005 11:24:12 -0000, "Mark English" <Mark.English at liffe.com> wrote:
>
>  
>
>>I'd like to write a Tkinter app which, given a class, pops up a
>>window(s) with fields for each "attribute" of that class. The user could
>>enter values for the attributes and on closing the window would be
>>returned an instance of the class. The actual application I'm interested
>>in writing would either have simple type attributes (int, string, etc.),
>>or attributes using types already defined in a c-extension, although I'd
>>prefer not to restrict the functionality to these requirements.
>>    
>>
Hmm, I missed the original post, but I'll jump in anyway:

    This sounds a heck of a lot like a property-editing system.  When
    creating a property-modeled system, the best approach is normally to
    use something that actually models the properties, rather than
    trying to guess at the metadata involved by poking around in an
    arbitrarily structured object.

    My BasicProperty system allows for this kind of interaction
    (property-sheets) using wxPython (rather than Tkinter) when using
    wxoo.  You declare classes as having a set of data-properties (which
    can have defaults or not, constraints or not, restricted data-types
    or not, friendly names or not, documentation or not).  Normally you
    create these classes as subclasses of a class that knows how to
    automatically assign __init__ parameters to properties, and knows
    how to tell (e.g.) wxoo about the properties of the class.

    Those same property classes also allow for editing properties of
    database rows in PyTable, but that isn't likely relevant to your
    case.  We've also used them internally to create a rather large
    web-based property-editing mechanism (applied to such databases),
    but again, not really relevant to the case at hand.

Anyway, if you aren't interested in BasicProperty for this task; another 
project on which I work, PyDispatcher provides fairly robust mechanism 
(called robustApply) for providing a set of possible arguments and using 
inspect to pick out which names match the parameters for a function in 
order to pass them in to the function/method/callable object.  That 
said, doing this for __init__'s with attribute values from an object's 
dictionary doesn't really seem like the proper way to approach the problem.

Good luck,
Mike

________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list