RIIA in Python 2.5 alpha: "with... as"

Diez B. Roggisch deets at nospam.web.de
Tue Apr 11 09:16:41 EDT 2006


>> No, it means that Python 2.5 supports 'resource initialisation is
>> acquisition', but that has nothing to do with the restricting the
>> lifetime of a variable.
> Sorry, I misworded the question - RIIA is indeed present at least by
> the reason that the examples from PEP pass. Agree, my problem is a bit
> different, and I a bit mixed up initialization/acquisition with
> lifetime blocks. So, seems that we indeed have one and still don't
> have another.
> Or maybe you have an idea how this can be fixed? The
> simplest way I see is putting all the "controlled" variables into a
> dedicated class... and do that each time for each block of variables I
> need control lifetime. Is there any simpler way?

Wouldn't a small surrounding function suffice? Something like this
(untested):


def whatever():
  def anon():
     with open('/etc/passwd', 'r') as f:
         for line in f:
             print line

Sure, not the nicest of all solutions. But if you really fear that f is
reused, it might help.

Diez



More information about the Python-list mailing list