Deprecate input()
Bengt Richter
bokr at oz.net
Tue Apr 16 17:46:37 EDT 2002
On 15 Apr 2002 13:58:11 -0500, Ian Bicking <ianb at colorstudy.com> wrote:
>On Mon, 2002-04-15 at 06:35, Magnus Lie Hetland wrote:
>> But (in the (very?) long run) I think that the name "input" should
>> then be used for what is now raw_input. I know -- breakage, and all
>> that... I just think it fairly odd to have *only* raw_input, since the
>> "raw" qualification then becomes fairly meaningless.
>
>Why not just make input into what it should be, a moderately-evaluating
>raw_input... like:
>
>def input(prompt=''):
> val = raw_input(prompt)
> for converter in [int, float, complex]:
> try:
> return converter(val)
> except ValueError:
> pass
> return val
>
>
>Of course, it gets more complex if you allow lists, dictionaries, and
>tuples... which you probably should. But that's certainly doable while
>still being safe.
>
You could also just add a sequence of validation conditions, e.g., (pseudocode, not tested):
def prompted_input(prompt='', *validation_seq):
val = [raw_input(prompt)]
for valcond in validation_seq:
valcond[0](val, valcond[1:]) #raises exception if cond violated
return val[0]
to be called something like (example shows more than usual checks ;-):
prompted_input(
'Enter a number: ',
(vfun_conv, [int], 'Error: %(theValue) is not an integer.'),
(vfun_min, 3, 'Need at least %(theMin)'),
(vfun_max, 8, 'Must be no more than %(theMax)'),
(vfun_sel, (3,7), 'Must select from %(theSet)'),
(mySpecialCheck, myArgs)
)
with vfun_xxx being builtin standard validation functions, but the list not
limited to those. Custom error text would be passed through via ValidationCheck exception.
Using [val] vs val allows possibility of repairing/modifying something in mySpecialCheck.
Regards,
Bengt Richter
More information about the Python-list
mailing list