Various clarifications. Mention Decimal context example. Clarify
that I don't like having files etc. implement __enter__ and __exit__ directly.
This commit is contained in:
parent
e9086b2485
commit
3df485114c
38
pep-0343.txt
38
pep-0343.txt
|
@ -57,6 +57,10 @@ Specification
|
|||
finally:
|
||||
abc.__exit__(exc)
|
||||
|
||||
Here, the variables 'abc' and 'exc' are internal variables and not
|
||||
accessible to the user; they will most likely be implemented as
|
||||
special registers or stack positions.
|
||||
|
||||
If the "as VAR" part of the syntax is omitted, the "VAR =" part of
|
||||
the translation is omitted (but abc.__enter__() is still called).
|
||||
|
||||
|
@ -114,6 +118,32 @@ Optional Generator Decorator
|
|||
A robust implementation of such a decorator should be made part of
|
||||
the standard library.
|
||||
|
||||
Other Optional Extensions
|
||||
|
||||
It would be possible to endow certain objects, like files,
|
||||
sockets, and locks, with __enter__ and __exit__ methods so that
|
||||
instead of writing
|
||||
|
||||
do locking(myLock):
|
||||
BLOCK
|
||||
|
||||
one could write simply
|
||||
|
||||
do myLock:
|
||||
BLOCK
|
||||
|
||||
I think we should be careful with this; it could lead to mistakes
|
||||
like
|
||||
|
||||
f = open(filename)
|
||||
do f:
|
||||
BLOCK1
|
||||
do f:
|
||||
BLOCK2
|
||||
|
||||
which does not do what one might think (f is closed when BLOCK2 is
|
||||
entered).
|
||||
|
||||
Examples
|
||||
|
||||
Several of these examples contain "yield None". If PEP 342 is
|
||||
|
@ -194,6 +224,10 @@ Examples
|
|||
do redirecting_stdout(f):
|
||||
print "Hello world"
|
||||
|
||||
This isn't thread-safe, of course, but neither is doing this
|
||||
same dance manually. In a single-threaded program (e.g., a
|
||||
script) it is a totally fine way of doing things.
|
||||
|
||||
6. A variant on opening() that also returns an error condition:
|
||||
|
||||
@do_template
|
||||
|
@ -226,6 +260,10 @@ Examples
|
|||
by default all signals are blocked. The implementation is left
|
||||
as an exercise to the reader.
|
||||
|
||||
8. Another use for this feature is the Decimal context. It's left
|
||||
as an exercise for the reader. (Mail it to me if you'd like to
|
||||
see it here.)
|
||||
|
||||
Copyright
|
||||
|
||||
This document has been placed in the public domain.
|
||||
|
|
Loading…
Reference in New Issue