[PyCrust] Attribute list hook
Brought to you by:
pobrien
From: Neil H. <nho...@bi...> - 2001-08-11 07:35:20
|
On the PythonCard list I've been pushing the use of __getattr__ and __setattr__ to allow access to widget properties through dot notation - self.components.btnTranslate.label = "Celsius to Fahrenheit" rather than self.components.getWidget("btnTranslate").setLabel("Celsius to Fahrenheit") One problem with this is that interactive shells such as PyCrust can't find out the names of these attributes as they are not in __dict__ even though the set of such attributes is normally well known. This is easy enough to solve in the context of PythonCard using PyCrust by adding some code (or stuffing __dict__ with the known attributes) but this is a generic problem that should be solved for use by all interactive shells. Has this been solved for any other shells? If not, I propose a convention which is to provide a getAttributeNames method on any class that wants to expose its dynamic (or semi-dynamic) attributes to shells. Thus class D11: def getAttributeNames(self): return ["woderwick", "woger", "weginald"] class D12: pass def attrList(d): try: print d.getAttributeNames() except AttributeError, x: print "Empty" Then interactively: >>> d = D11() >>> attrList(d) ['woderwick', 'woger', 'weginald'] >>> e = D12() >>> attrList(e) Empty If there is resistance to using exceptions here, then a more complex mechanism that involves checking the __class__ and __class__.__bases__ could be used or a marker attribute added. I'll leave this message here for a day and if there are no big problems will copy it to comp.lang.python to see if there are more opinions. Neil |