Re: [Python-Dev] Proposed changes to PEP 343
On 10/7/05, Fredrik Lundh <fredrik@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
Jason Orendorff wrote:
A contextmanager is a function that returns a new context manager.
Okay, that last bit is weird.
If the name of the decorator is to be 'contextmanager', it really needs to say something like The contextmanager decorator turns a generator into a function that returns a context manager. So maybe the decorator should be called 'contextmanagergenerator'. Or perhaps not, since that's getting rather too much of an eyeful to parse... -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg.ewing@canterbury.ac.nz +--------------------------------------+
Greg Ewing wrote:
Jason Orendorff wrote:
A contextmanager is a function that returns a new context manager.
Okay, that last bit is weird.
If the name of the decorator is to be 'contextmanager', it really needs to say something like
The contextmanager decorator turns a generator into a function that returns a context manager.
So maybe the decorator should be called 'contextmanagergenerator'. Or perhaps not, since that's getting rather too much of an eyeful to parse...
Strictly speaking this fits in with the existing confusion of "generator factory" and "generator": Py> def g(): ... yield None ... Py> type(g) <type 'function'> Py> type(g()) <type 'generator'> Most people would call "g" a generator, even though its really just a factory function that returns generator objects. So technically, the "contextmanager" decorator turns a generator factory function into a context manager factory function. But its easier to simply say that the decorator turns a generator into a context manager, even if that's technically incorrect. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://boredomandlaziness.blogspot.com
participants (3)
-
Greg Ewing -
Jason Orendorff -
Nick Coghlan