3 XXX This is very much a work in progress.
9 class MetaMethodWrapper
:
11 def __init__(self
, func
, inst
):
14 self
.__name
__ = self
.func
.__name
__
16 def __call__(self
, *args
, **kw
):
17 return apply(self
.func
, (self
.inst
,) + args
, kw
)
21 __methodwrapper__
= MetaMethodWrapper
# For derived helpers to override
23 def __helperinit__(self
, formalclass
):
24 self
.__formalclass
__ = formalclass
26 def __getattr__(self
, name
):
27 # Invoked for any attr not in the instance's __dict__
29 raw
= self
.__formalclass
__.__getattr
__(name
)
30 except AttributeError:
32 ga
= self
.__formalclass
__.__getattr
__('__usergetattr__')
33 except (KeyError, AttributeError):
34 raise AttributeError, name
36 if type(raw
) != types
.FunctionType
:
38 return self
.__methodwrapper
__(raw
, self
)
42 """A generic metaclass.
44 This can be subclassed to implement various kinds of meta-behavior.
48 __helper__
= MetaHelper
# For derived metaclasses to override
52 def __init__(self
, name
, bases
, dict):
54 ga
= dict['__getattr__']
58 dict['__usergetattr__'] = ga
59 del dict['__getattr__']
61 self
.__bases
__ = bases
62 self
.__realdict
__ = dict
65 def __getattr__(self
, name
):
67 return self
.__realdict
__[name
]
69 for base
in self
.__bases
__:
71 return base
.__getattr
__(name
)
72 except AttributeError:
74 raise AttributeError, name
76 def __setattr__(self
, name
, value
):
78 self
.__dict
__[name
] = value
80 self
.__realdict
__[name
] = value
82 def __call__(self
, *args
, **kw
):
83 inst
= self
.__helper
__()
84 inst
.__helperinit
__(self
)
86 init
= inst
.__getattr
__('__init__')
87 except AttributeError:
93 Meta
= MetaClass('Meta', (), {})
98 def __init__(self
, *args
):
99 print "__init__, args =", args
101 print "m1(x=%r)" % (x
,)
107 def __getattr__(self
, name
):
108 if name
[:2] == '__': raise AttributeError, name
109 return "getattr:%s" % name
117 if __name__
== '__main__':