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

Jan Kaliszewski zuo at chopin.edu.pl
Sat Jul 18 00:40:16 CEST 2009


17-07-2009 o 14:12 Ben Finney <ben+python at benfinney.id.au> wrote:

> David Stanek <dstanek at dstanek.com> writes:
>
>> This thread is a little confusing to me. I am failing to see how the
>> where block makes anything clearer.
>
> +1. I haven't found that any of the examples improve the clarity of the
> code over using a named function.

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


...is more logical and readable for me, than:

class SomeClass:
      ...

      def _weirdtrans(self, obj):
          # some transformations useful
          # only for somefilter method

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

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

          alts = map(self._weirdtrans, objects)
          return map(action, objects, args, alts)


17-07-2009, 22:28 Gerald Britton <gerald.britton at gmail.com> wrote:

> which I would like to write:
>
> mylist =  [item in f if g(item)] where:

Obviously you ment: "[item for item if g(item)] where:", didn't you?
In that particular case you could write: filter(g, f).
Anyway still I'd prefer:

mylist = filter(g, f) where:
      f = self.someobject.get_generator()
      g = self.important_test

Than:

f = self.someobject.get_generator()
g = self.important_test
mylist = filter(g, f) where:

> then "f" and "g" pollute the calling context's namespace.

+1

Generally one of main applications of 'where' would be using it
for functions what use function(s) as argument(s) (map, filter
et consortes...).

After all it's something like lambda, but:
* more beautiful (IMHO),
* not limited to expression.

Regards,

-- 
Jan Kaliszewski <zuo at chopin.edu.pl>



More information about the Python-ideas mailing list