ANN:modeler __getattr__ for modules
Wiktor Sadowski
Wiktor Sadowski" <art@wiktorsadowski.com
Fri, 20 Jun 2003 07:09:25 +0200
modeler
======
Some posts on class-like attribute access for modules
encourged me to get some code from my not-yet-released viz module
and make it available as a standalone Python C extension module.
Modeler enables dynamic attribute creation/access by simply adding
a __gettattr__ function to a module. Modeler doesn't replace modules
with class instances,actually it doesn't make any changes to modules.
Some more details and modeler.pyd for Python2.2
can be found at:
http://www.wiktorsadowski.com/Python
Examples of simple module-class-factory and importing two or more modules
from one dll :
http://www.wiktorsadowski.com/Python/modsample.html
As for the source code:
I will try to make it available later this month.
Have fun,
Wiktor Sadowski
add to site.py:
import modeler
===========
dynmodule.py
==========
def __getattr__(thismod,name):
if name in thismod.__dict__.keys():
return thismod.__dict__[name]
else:
# in more useful cases:
#thismod.__dict__[name]=anAttr
#return anAttr
return "Default Attr"
======================================================
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> from dynmod import whatever1,whatever2
>>> whatever1
'Default Attr'
>>> whatever2
'Default Attr'
>>> import dynmod
>>> dynmod.foo
'Default Attr'
>>> dynmod.spam
'Default Attr'
>>>