Subclassing the min() build-in function?
Fredrik Lundh
fredrik at pythonware.com
Wed Nov 6 12:43:35 EST 2002
Peter Lorenzen wrote:
> I am using Jython to calculate a string like this: "2+3+min(1,2,3)"
> from my Java pro-gram. This works beautifully. But my problem is that
> if a user wants to calculate min(1) Python considers this an error. I
> want it to return 1. If I could figure out how to subclass the min
> function
(subclass? since when is "min" a class?)
to override a builtin function, you can do something like
this:
import __builtin__
builtin_min = __builtin__.min
def min(*args):
try:
return builtin_min(*args)
except TypeError:
if len(args) == 1:
return args[0]
raise
__builtin__.min = min
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list