[Chicago] class continued, or metaclassing + framehackery, or an exercise in never using this because it's BAD... but fun.
Kumar McMillan
kumar.mcmillan at gmail.com
Sat Mar 29 06:37:44 CET 2008
On Fri, Mar 28, 2008 at 8:39 PM, Kumar McMillan
<kumar.mcmillan at gmail.com> wrote:
> On Fri, Mar 28, 2008 at 4:51 PM, Ian Bicking <ianb at colorstudy.com> wrote:
> >
> > OK, I haven't upgraded, so this is all untested, but to make the
> > decorator more awesome:
>
> after fixing print('yo!') I still got a syntax error on the
> @monkeypatch(Widget) using python3.0 (alpha 2).
duh. it just had a trailing colon `:' causing the error. This works.
It prints "yo! \n hey! ":
def monkeypatch(new_cls):
def do_the_patch(cls):
return type('Intermediate%s' % cls.__name__, (new_cls,) +
cls.__bases__, cls.__dict__.copy())
return do_the_patch
class WidgetYo:
def onclick(self):
print('yo!')
@monkeypatch(WidgetYo)
class Widget:
def onclick(self):
super(Widget, self).onclick()
print('hey!')
w = Widget()
w.onclick()
...altering cls.__bases__ in place doesn't seem to be possible
(TypeError: __bases__ assignment: 'WidgetYo' deallocator differs from
'object').
K
More information about the Chicago
mailing list