[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 02:39:49 CET 2008


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).  Strange, though,
since it looks like this went through a while ago:
http://www.python.org/dev/peps/pep-3129/ .  Maybe it needs a future
import or my alpha is too far out of date.

metaclasses are a mess... can't wait for class decorators!

>
>
>  def monkeypatch(cls):
>      def do_the_patch(new_cls):
>          fake_class = type('Intermediate%s' % cls.__name__,
>  cls.__bases__, cls.__dict__)
>          cls.__dict__.clear()
>          cls.__bases__ += (fake_class,)
>          cls.__dict__.update(new_cls.__dict__)
>
>          return cls
>      return do_the_patch
>
>  Then, maybe you could do:
>
>  class Widget(SomethingMoreParent):
>      def onclick(self):
>          print 'yo!'
>
>  w = Widget()
>
>  @monkeypatch(Widget):
>  class Widget:
>      def onclick(self):
>          super().onclick()
>          print 'hey!'
>
>  w.onclick()
>  # yo!
>  # hey!
>
>
>
>  I'm not sure this will work, because I don't know how py3k super()
>  works.  And I'm fuzzy on how py2k super() works, as except for the class
>  decorator syntax itself (which is just equivalent to
>  monkeypatch(Widget)(Widget)) this is all valid in Python 2.  I think
>  super(Widget, self).onclick() would actually work right.
>
>
>
>    Ian
>  _______________________________________________
>  Chicago mailing list
>  Chicago at python.org
>  http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list