[Python-Dev] Python Enhancement Proposals (PEPs)

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Thu, 13 Jul 2000 21:39:56 +0200


mal wrote:
> Uhm, why should marry() be defined for a single argument ?

because map is defined for a single argument?

> ... the code is there. I'd say: use it :-)

well, yes.  if zip/marry/whatever is just a map without
the first argument, that's definitely true ;-)

</F>

in case someone wants to play:

Index: Python/bltinmodule.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.170
diff -u -r2.170 bltinmodule.c
--- Python/bltinmodule.c 2000/07/12 13:03:02 2.170
+++ Python/bltinmodule.c 2000/07/13 19:32:37
@@ -963,9 +963,10 @@
=20
=20
 static PyObject *
-builtin_map(self, args)
+mapzip(self, args, zip)
  PyObject *self;
  PyObject *args;
+ int zip;
 {
  typedef struct {
   PyObject *seq;
@@ -977,20 +978,31 @@
  sequence *seqs =3D NULL, *sqp;
  int n, len;
  register int i, j;
+ int first_seq;
=20
  n =3D PyTuple_Size(args);
- if (n < 2) {
-  PyErr_SetString(PyExc_TypeError,
-    "map() requires at least two args");
-  return NULL;
+ if (zip) {
+  if (n < 1) {
+   PyErr_SetString(PyExc_TypeError,
+    "zip() requires at least one arg");
+   return NULL;
+  }
+  func =3D Py_None;
+  first_seq =3D 0;
+ } else {
+  if (n < 2) {
+   PyErr_SetString(PyExc_TypeError,
+     "map() requires at least two args");
+   return NULL;
+  }
+  func =3D PyTuple_GetItem(args, 0);
+  first_seq =3D 1;
+  n--;
  }
=20
- func =3D PyTuple_GetItem(args, 0);
- n--;
-
  if (func =3D=3D Py_None && n =3D=3D 1) {
   /* map(None, S) is the same as list(S). */
-  return PySequence_List(PyTuple_GetItem(args, 1));
+  return PySequence_List(PyTuple_GetItem(args, first_seq));
  }
=20
  if ((seqs =3D PyMem_NEW(sequence, n)) =3D=3D NULL) {
@@ -1002,20 +1014,15 @@
   int curlen;
   PySequenceMethods *sqf;
 =20
-  if ((sqp->seq =3D PyTuple_GetItem(args, i + 1)) =3D=3D NULL)
+  if ((sqp->seq =3D PyTuple_GetItem(args, i + first_seq)) =3D=3D NULL)
    goto Fail_2;
=20
   sqp->sqf =3D sqf =3D sqp->seq->ob_type->tp_as_sequence;
   if (sqf =3D=3D NULL ||
       sqf->sq_length =3D=3D NULL ||
-      sqf->sq_item =3D=3D NULL)
-  {
-   static char errmsg[] =3D
-       "argument %d to map() must be a sequence object";
-   char errbuf[sizeof(errmsg) + 25];
-
-   sprintf(errbuf, errmsg, i+2);
-   PyErr_SetString(PyExc_TypeError, errbuf);
+      sqf->sq_item =3D=3D NULL) {
+   PyErr_Format(PyExc_TypeError,
+    "argument %d to map() must be a sequence object", i+2);
    goto Fail_2;
   }
=20
@@ -1118,6 +1125,16 @@
  return NULL;
 }
=20
+
+static PyObject *
+builtin_map(self, args)
+ PyObject *self;
+ PyObject *args;
+{
+ return mapzip(self, args, 0);
+}
+
+
 static char map_doc[] =3D
 "map(function, sequence[, sequence, ...]) -> list\n\
 \n\
@@ -1130,6 +1147,22 @@
=20
=20
 static PyObject *
+builtin_zip(self, args)
+ PyObject *self;
+ PyObject *args;
+{
+ return mapzip(self, args, 1);
+}
+
+
+static char zip_doc[] =3D
+"zip(sequence[, sequence, ...]) -> list\n\
+\n\
+Return a list of the items of the sequence (or a list of tuples if =
more\n\
+than one sequence).";
+
+
+static PyObject *
 builtin_setattr(self, args)
  PyObject *self;
  PyObject *args;
@@ -2327,6 +2360,7 @@
  {"unichr", builtin_unichr, 1, unichr_doc},
  {"vars", builtin_vars, 1, vars_doc},
  {"xrange", builtin_xrange, 1, xrange_doc},
+ {"zip",  builtin_zip, 1, zip_doc},
  {NULL,  NULL},
 };