Life's better without builtins? (was: Life's better without braces)
Niels Diepeveen
niels at endea.demon.nl
Fri Feb 25 09:29:01 EST 2000
Gerrit Holl schreef:
>
> <quote name="Andrew M. Kuchling" date="951150482">
> > [2] I occasionally think about this problem. The setup: GvR was far
> > too profligate in adding functions to bltinmodule.c that could have
> > been written in pure Python instead. So, how many built-in functions
> > can you re-implement in pure Python?
>
> These are possible:
>
> abs, callable, chr, delattr, divmod,
> execfile, filter, getattr, hex, input, int, isinstance, issubclass,
> len, list, long, map, max, min, oct, range, raw_input, reduce, reload,
> repr, setattr, tuple, vars.
Do you have some source code of this? I can't think of a way to do
reload() or tuple().
>
> These aren't:
>
> apply, buffer, coerce, compile, complex, dir, eval, exit, globals, hash,
> id, intern, locals, round, slice, type, xrange
I think you're a bit pessimistic there. For example:
_interns = {}
def intern(s):
try:
return _interns[s]
except KeyError:
_interns[s] = s
return s
class _slice_hack_class:
def __getitem__(self, n):
return n
_slice_hack = _slice_hack_class()
def slice(*args):
if len(args) == 0:
raise TypeError('slice requires at least 1 argument; 0 given')
elif len(args) == 1:
return _slice_hack[:args[0]:]
elif len(args) == 2:
return _slice_hack[args[0]:args[1]:]
elif len(args) == 3:
return _slice_hack[args[0]:args[1]:args[2]]
else:
raise TypeError('slice requires at most 3 arguments; ' +
`len(args)` + ' given')
def round(val, prec = 0):
if val >= 0.0:
return 10.0 ** (-prec) * float(long(val * 10.0 ** prec + 0.5))
else:
return 10.0 ** (-prec) * float(long(val * 10.0 ** prec - 0.5))
--
Niels Diepeveen
Endea automatisering
More information about the Python-list
mailing list