[Python-Dev] Proposed changes to PEP 343 (original) (raw)
Jason Orendorff jason.orendorff at gmail.com
Tue Oct 11 22:15:03 CEST 2005
- Previous message: [Python-Dev] Python-Dev Digest, Vol 27, Issue 44
- Next message: [Python-Dev] Proposed changes to PEP 343
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 10/7/05, Fredrik Lundh <fredrik at pythonware.com> wrote:
the whole concept might be perfectly fine on the "this construct corre- sponds to this code" level, but if you immediately end up with things that are not what they seem, and names that don't mean what the say, either the design or the description of it needs work.
("yes, I know you can use this class to manage the context, but it's not really a context manager, because it's that method that's a manager, not the class itself. yes, all the information that belongs to the context are managed by the class, but that doesn't make... oh, shut up and read the PEP")
Good points... Maybe it is the description that needs work.
Here is a description of iterators, to illustrate the parallels: An object that has an iter method is iterable. It can plug into the Python 'for' statement. obj.iter() returns an iterator. An iterator is a single-use, forward-only view of a sequence. 'for' calls iter() and uses the resulting iterator's next() method.
(This is just as complicated as PEP343+changes, but not as
mindboggling, because the terminology is better. Also because
we're used to iterators.)
Now contexts, per PEP 343 with Nick's proposed changes: An object that has a with method is a context. It can plug into the Python 'with' statement. obj.with() returns a context manager. A context manager is a single-use object that manages a single visit into a context. 'with' calls with() and uses the resulting context manager's enter() and exit() methods.
A contextmanager is a function that returns a new context manager.
Okay, that last bit is weird. But note that PEP 343 has this oddness even without the proposed changes. Perhaps either "context manager" or contextmanager should be renamed, regardless of whether Nick's changes are accepted.
With the changes, context managers will be (conceptually) single-use. So maybe a different term might be appropriate. Perhaps "ticket". "A ticket is a single-use object that manages a single visit into a context."
-j
- Previous message: [Python-Dev] Python-Dev Digest, Vol 27, Issue 44
- Next message: [Python-Dev] Proposed changes to PEP 343
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]