[Python-ideas] Where-statement (Proposal for function expressions)

Ben Finney ben+python at benfinney.id.au
Sat Jul 18 01:52:22 CEST 2009


"Jan Kaliszewski" <zuo at chopin.edu.pl> writes:

> And what about that:
> 
> class SomeClass:
>      ...
> 
>      def summarize_proust(self, objects, args, method_name)
>          "Summarize Proust in 15 seconds"
> 
>          return map(action, objects, args, alts) where:
> 
>              def action(obj, arg, alt):
>                  result = getattr(item, method_name)(arg) or alt
>                  obj.set_last_result(result)
>                  return result
> 
>              alts = map(weirdtrans, objects) where def weirdtrans(obj):
>                  # some transformations not very
>                  # useful in more general context

You then make an invalid comparison of this versus making ‘weirdtrans’
an unnecessary instance method. But the correct equivalent of your
example above is to make another local function::

    class SomeClass:
        # …

        def summarize_proust(self, objects, args, method_name):
            """ Summarize Proust in 15 seconds. """

            def weirdtrans(obj):
                # some transformations not very
                # useful in more general context

            alts = map(weirdtrans, objects)

            def action(obj, arg, alt):
               result = getattr(item, method_name)(arg) or alt
               obj.set_last_result(result)
               return result

            return map(action, objects, args, alts)

That seems more straightforward to me. Certainly I don't find the one
with ‘where’ any clearer.

-- 
 \                   “Holy unrefillable prescriptions, Batman!” —Robin |
  `\                                                                   |
_o__)                                                                  |
Ben Finney




More information about the Python-ideas mailing list