[Chicago] class continued, or metaclassing + framehackery, or an exercise in never using this because it's BAD... but fun.

Pete pfein at pobox.com
Fri Mar 28 22:06:30 CET 2008


Oh, I totally missed:

> > class Widget(object):


> > class Widget(continued):


So the idea is that you can "redefine" an existing class to add  
methods to it?  What's the benefit over monkeypatching?

FWIW, I'm not sure "continue" is the best verbiage here... might as  
well call it MonkeyMeta or somesuch.

On the whole, neato.  I might borrow the getframe-in-metaclass  
technique for a problem where arbitrary block syntax would be nice.   
Too bad the make PEP got rejected, I find a use for it every once in a  
while: http://www.python.org/dev/peps/pep-0359/

On Mar 28, 2008, at 3:18 PM, Paul Smith wrote:

> If I read this correctly, it's not unlike the "open classes" that  
> the Ruby-iphiles are always raving about and abusing.
>
> Cheers,
> -Paul
>
> On Fri, Mar 28, 2008 at 2:06 PM, Pete <pfein at pobox.com> wrote:
> I don't get it.
>
> On Mar 27, 2008, at 5:07 PM, Brantley Harris wrote:
> > import sys
> >
> > class ContinueMeta(type):
> >    def __new__(cls, name, bases, dct):
> >        if name == 'continued':
> >            return type.__new__(cls, name, bases, dct)
> >        existing = sys._getframe(1).f_locals[name]
> >        for k, v in dct.items():
> >            setattr(existing, k, v)
> >        return existing
> >
> > class continued(object):
> >    __metaclass__ = ContinueMeta
> >
> > ### Testing ###
> > class Widget(object):
> >    def __init__(self):
> >        print "Widget created!"
> >
> > class Widget(continued):
> >    def hello(self):
> >        print "Hello world!"
> >
> > ----------
> >>>> w = Widget()
> >>>> w.hello()
> > Widget created!
> > Hello world!
> > _______________________________________________
> > Chicago mailing list
> > Chicago at python.org
> > http://mail.python.org/mailman/listinfo/chicago
>
> Pete
> pfein at pobox.com
>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>
>
> -- 
> Paul Smith
> http://www.pauladamsmith.com/  
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago

Pete
pfein at pobox.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20080328/8c63af40/attachment.htm 


More information about the Chicago mailing list