[Python-checkins] python/nondist/peps pep-0309.txt,1.2,1.3
goodger@users.sourceforge.net
goodger@users.sourceforge.net
Mon, 10 Mar 2003 20:49:47 -0800
Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1:/tmp/cvs-serv425
Modified Files:
pep-0309.txt
Log Message:
update from Peter Harris, plus spell-check & edit
Index: pep-0309.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0309.txt,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** pep-0309.txt 19 Feb 2003 00:46:51 -0000 1.2
--- pep-0309.txt 11 Mar 2003 04:49:44 -0000 1.3
***************
*** 9,31 ****
Created: 08-Feb-2003
Python-Version: 2.4
! Post-History: 10-Feb-2003
Abstract
! =========
! This proposal is for a built-in closure or curry type for Python that
! allows a new callable to be constructed from another callable and a
! partial argument list (including positional and keyword arguments). A
! concise syntax shorthand for curried functions is suggested
! (tentatively).
Note: after feedback on comp.lang.python, I am persuaded that the most
! accurate term for this is a 'curry', so the terminology has been
! amended since the first version of this PEP.
Motivation
! ===========
Curried functions are useful as functional 'sections' or as convenient
--- 9,32 ----
Created: 08-Feb-2003
Python-Version: 2.4
! Post-History: 10-Feb-2003, 27-Feb-2003
Abstract
! ========
! This proposal is for a standard curry type for Python that
! allows a new callable to be constructed from a callable and a
! partial argument list (including positional and keyword arguments).
Note: after feedback on comp.lang.python, I am persuaded that the most
! accurate term for this is a 'curry' rather than a 'closure', so the
! terminology has been amended since the first version of this PEP.
!
! I propose a standard library module called "functional", to hold useful
! higher-order functions, including the curry() class.
Motivation
! ==========
Curried functions are useful as functional 'sections' or as convenient
***************
*** 50,54 ****
Rationale
! ==========
Here is one way to do a curry in Python::
--- 51,55 ----
Rationale
! =========
Here is one way to do a curry in Python::
***************
*** 60,65 ****
def __call__(self, *args, **kw):
! d = self.kw.copy()
! d.update(kw)
return self.fn(*(self.args + args), **d)
--- 61,69 ----
def __call__(self, *args, **kw):
! if self.kw:
! d = self.kw.copy()
! d.update(kw)
! else:
! d = kw
return self.fn(*(self.args + args), **d)
***************
*** 79,93 ****
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549.
- Tentative Syntax Proposal
- ==========================
! I know syntax proposals have the odds stacked against them, and
! introducing new punctuation characters is frowned upon, but I think
! curries may be a sufficiently powerful abstraction to deserve it.
! I suggest the syntax ``fn@(*args, **kw)``, meaning the same as
! ``curry(fn, *args, **kw)``. I have no idea what havoc this would
! wreak on the parser.
At least there are no backwards-compatibility issues because the @
--- 83,96 ----
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549.
+ Update: It seems likely that a standard library implementation would
+ be in Python, and would have to prove its worth there before making
+ it into the built-ins.
! Abandoned Syntax Proposal
! =========================
! I originally suggested the syntax ``fn@(*args, **kw)``, meaning the same
! as ``curry(fn, *args, **kw)``.
At least there are no backwards-compatibility issues because the @
***************
*** 120,126 ****
nextarg = sys.argv.pop@(0)
Feedback from comp.lang.python
! ===============================
Among the opinions voiced were the following (which I summarise):
--- 123,132 ----
nextarg = sys.argv.pop@(0)
+ It has not been well-received, so I am not pursuing this as a serious
+ proposal.
+
Feedback from comp.lang.python
! ==============================
Among the opinions voiced were the following (which I summarise):
***************
*** 137,141 ****
library.
! * It maybe isn't useful enough to be in the builtins.
I agree that lambda is usually good enough, just not always. And I
--- 143,155 ----
library.
! * It maybe isn't useful enough to be in the built-ins.
!
! * The idea of a module called ``functional`` was well received, and
! there are other things that belong there (for example function
! composition).
!
! * For completeness, another curry class that appends curried arguments
! after those supplied in the function call (maybe called
! ``rightcurry``) has been suggested.
I agree that lambda is usually good enough, just not always. And I
***************
*** 152,162 ****
amended this PEP accordingly.
- I think it's best as a builtin type rather than in a separate standard
- library module, because it's simple and general enough. It may not be
- an idiom that is very common in Python programming at the moment, but
- I think that's because you have to code it yourself if you want it.
- If added as a built-in feature, we would soon be wondering how we
- managed without it.
-
Carl Banks posted an implementation as a real functional closure::
--- 166,169 ----
***************
*** 177,205 ****
cdef class curry:
cdef object fn, args, kw
def __init__(self, fn, *args, **kw):
self.fn=fn
! self.args=args
! self.kw = kw
def __call__(self, *args, **kw):
! if self.kw: # from Python Cookbook version
! d = self.kw.copy()
d.update(kw)
! else:
! d=kw
return self.fn(*(self.args + args), **d)
! but I'm guessing that there would be minimal performance improvement
! since it compiles to a load of Python API calls.
Summary
! ========
! I maintain that curry should be a built-in, with the semantics as
! described, whether as a function or a class.
The @ syntax proposal is withdrawn.
--- 184,225 ----
cdef class curry:
+
cdef object fn, args, kw
+
def __init__(self, fn, *args, **kw):
self.fn=fn
! self.args=args
! self.kw = kw
def __call__(self, *args, **kw):
! if self.kw: # from Python Cookbook version
! d = self.kw.copy()
d.update(kw)
! else:
! d=kw
return self.fn(*(self.args + args), **d)
! The performance gain in Pyrex is less than 100% over the nested function
! implementation, since to be fully general it has to operate by Python API
! calls. Any C implementation will be unlikely to be much faster, so the
! case for a builtin coded in C is not very strong.
!
Summary
! =======
! I prefer that curry should be a built-in, with the semantics as
! described, whether as a function or a class. However, it should do its
! apprenticeship in the standard library first.
!
! The standard library module ``functional`` should contain ``curry`` and
! ``rightcurry`` classes, and any other higher-order functions the community
! want. These other functions fall outside this PEP though.
The @ syntax proposal is withdrawn.
+
+ Since this proposal is now much less ambitious, I'd like to aim for
+ inclusion in Python 2.3.