[Python-Dev] PEP 326 (quick location possibility) (original) (raw)

Josiah Carlson jcarlson at uci.edu
Mon Jan 26 20:32:16 EST 2004


What does everyone think of the following?

A few people have suggested that the Max and Min objects be available as the result of calls to max() and min(), this would solve the location and name problem, and shouldn't break any old code.

Current behavior of min and max:

  1. without arguments raises a TypeError
  2. with a single non-sequence argument raises a TypeError
  3. with an empty sequence as an argument raises a ValueError
  4. with one argument that is a non-empty sequence returns the min or max of the sequence
  5. with more than one argument returns the min or max of *args

If we assume that no one is calling min or max without arguments in order to raise a TypeError (which seems like a reasonable assumption), then replacing the TypeError exception for behavior 1 with the following seems reasonable: min() -> Min max() -> Max

It is also easy to wrap the current min/max functions in order to return the proper objects.

_min = min def min(*args): if args: return _min(*args) return Min

If such behavior is desired, it would probably make sense to alter the behavior of min_max() in bltinmodule.c to return pointers to the objects: (line 1094-1095 replaced with what is below) else if (!PyArg_UnpackTuple(args, (op==Py_LT) ? "min" : "max", 1, 1, &v)) { v = (op=Py_LT) ? PyMinObject : PyMaxObject; Py_INCREF(v); return v }

By extension, it would make sense to alter the objects Min and Max to have their string representation be 'min()' and 'max()' respectively.

The above 5-line modification coupled with the 5 line modification to PyObject_Compare (as posted in my previous post to python-dev which shows the replacement of line 758 in object.c with 5 others), and a minimal C implementation of the Min and Max objects (hopefully less than ~50 lines), could bring the conceptual Min and Max objects into reality. They would also invariably be smaller or larger than all other objects, due to modifications to the interpreter.

They would additionally not pollute any namespace (only being accessable by min() or max()) , and be virutally self-documenting (min() returns global minimum, max() returns global maximum).

What does everyone think? Thanks for reading,

5-line replace of 758 in object.c included below for reference: if (v == PyMinObject || w == PyMaxObject) return -1; else if (v == PyMaxObject || w == PyMinObject) return 1; else if (Py_EnterRecursiveCall(" in cmp"))



More information about the Python-Dev mailing list