Class introspection and dynamically determining function arguments

Nick Coghlan ncoghlan at iinet.net.au
Fri Jan 21 08:33:04 EST 2005


Diez B. Roggisch wrote:
> Nick Coghlan wrote:
> 
>>If this only has to work for classes created for the purpose (rather than
>>for an arbitrary class):
>>
> 
> 
> Certainly a step into the direction I meant - but still missing type
> declarations. And that's what at least I'd like to see - as otherwise you
> don't know what kind of editing widget to use for a property.

Hmm, true. You really need a name:type dict to define each class that is going 
to be generated.

Perhaps the simplest way is to require all such classes to have a "getExample" 
class method that produces a fully populated example instance (making it a class 
method means that you shouldn't need to override it in a subclass if you don't 
change the signature of __init__).

Then the widget generator doesn't need to care about *how* that default example 
gets generated, and can be something simple like:

build_widget(name, data_type):
   ...

build_widget_list(cls):
   example = cls.getExample()
   widgets = []
   for attr, value in example.__dict__:
     widgets.append(build_widget(attr, type(value)))
   return widgets

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list