Class introspection and dynamically determining function arguments

Mark English Mark.English at liffe.com
Fri Jan 21 05:39:31 EST 2005


> From: "Mark English" <Mark.English at liffe.com>
> 
> 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.

<------------------------>
From: "Diez B. Roggisch" <deetsNOSPAM at web.de>
> whatfor is it planned?

<------------------------>
From: Nick Coghlan <ncoghlan at iinet.net.au
> If this only has to work for classes created for the purpose
> (rather than for an arbitrary class):
<------------------------>

Although I'm writing it for a specific need with a specific set of
classes, I was asking here to find a more general, robust approach. So
handling an arbitrary class would be preferable.

> This is the only way to go, as python has no attribute declarations as
static compiled languages have
The classes I'm dealing with do have attributes since they're
C-Extension types with attributes provided in the "tp_getset" slot. So
this is one case where I can query the class for attributes without
having to create an instance. Thanks for making me think of that, since
looking in the class's __dict__ pretty much gives me what I want. I'm
not sure that that's something I can rely on though which is exactly the
sort of issue where I'd like to know if there's a right way (e.g. an
existing module) to ask a class for its attributes, without poking
around in __dict__ directly and doing some type checking. I guess in a
way I'm asking for introspection with a greater degree of granularity
than "dir".


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary companies.
-----------------------------------------------------------------------




More information about the Python-list mailing list