annonymous functions -- how to

Fredrik Lundh fredrik at pythonware.com
Fri May 6 02:44:53 EDT 2005


Dave Benjamin wrote:

> In this case, having to name these callback functions is tiring and
> awkward, and (IMHO) disrupts the flow of my function:

so name them all "func" or "next" or something, so you don't have
to think.  once the object is bound, the name is irrlevant.

> def add_thingy():
>      def next_thingy_id_func(thingy_id):
>          print 'got thingy id:', thingy_id
>          def next_doodad_id_func(doodad_id):
>              print 'got doodad id:', doodad_id
>              def new_thingy_doodad_func(thingy_doodad):
>                  print 'thingy doodad created, froobling...'
>                  frooble(thingy_doodad)
>                  print 'froobling complete'
>              with_new_thingy_doodad(thingy_id, doodad_id,
>                                     new_thingy_doodad_func)
>          with_next_doodad_id(next_doodad_id_func)
>      with_next_thingy_id(next_thingy_id_func)

there's also:

    def add_thingy(self):

        yield get_new_thingy_id; thingy_id = self.result

        print 'got thingy id:', thingy_id

        yield get_new_doodad_id; doodad_id = self.result

        print 'got doodad id:', doodad_id

        yield get_new_thingy_doodad; thingy_doodad = self.result

        print 'thingy doodad created, froobling...'
        frooble(thingy_doodad)
        print 'froobling complete'

</F>






More information about the Python-list mailing list