[Python-ideas] With clauses for generator expressions

Serhiy Storchaka storchaka at gmail.com
Fri Nov 16 11:29:17 CET 2012

On 16.11.12 11:09, Andrew Barnert wrote:
> With my suggested idea, the last 5 lines could be replaced by this:
>          self.send_async(self.censor(line) with open(path, 'r') as file for line
> in file)

    self.send_async(self.censor(line) for line in open(path, 'r'))


    self.send_async(map(self.censor, open(path, 'r')))

This is *not worse* than your first example

    self.send_async(open(path, 'r'))

How do you write a managed uncensored variant? You can use the wrapper suggested by Mathias Panzenböck.

    self.send_async(managed(open(path, 'r')))
    self.send_async(self.censor(line) for line in managed(open(path, 'r')))

It is easy, clear, universal and requires no changes to syntax.

More information about the Python-ideas mailing list