"Aaron Brady" <[EMAIL PROTECTED]> wrote: >On Nov 26, 10:41 pm, "Hendrik van Rooyen" <[EMAIL PROTECTED]> >wrote: >> "Steven D'Aprano" <steau> wrote: >> >> >> >> > Well, I don't know about "any problem". And it's not so much about >> > whether metaprograms can solve problems that can't be solved by anything >> > else, as whether metaprograms can solve problems more effectively than >> > other techniques. >> >> > If you include factory functions, class factories, the builder design >> > pattern, metaclasses, etc. as "metaprogramming", then I use it all the >> > time, and find it an excellent technique to use. >> >> > But if you mean using a Python program to generate Python source code, >> > then I can't think of any time I used it. Which doesn't mean that others >> > don't find it helpful, only that I haven't yet. >> >> I am using the term in the restricted sense of Python writing Python source. >> >> Given that, can anybody think of an example that you could not do with >> a class? (excepting the "stored procedure" aspect) >> >> Or can I claim a new a new meta - rule - I would call it van Rooyen's folly... > >The example I think of is the Visitor Pattern of Gamma and all. One >class's method calls another's method with its own class's name in the >name. > >class Visitor: > def visit_A( self, arg ):... > def visit_B( self, arg ):... > >class A: > def visit( self, vis ): > vis.visit_A( self ) > >class B: > def visit( self, vis ): > vis.visit_B( self ) > >As you can see, the 'visit' method is mechanical for classes A and B. >One might want to autogenerate those in some languages, but Python has >introspection: > >class BaseAB: > def visit( self, vis ): > getattr( vis, 'visit_%s'% self.__class__.__name__ )( self ) > >And it's easier to modify the default behavior this way than in >autogenerated code too.
This is kind of the wrong way around - this is an example of OO jiggery pokery that will be difficult to do by generating the source code - are there things that can be done by generating the source code that cannot be done with OO? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list