[Tutor] Decoding
Kent Johnson
kent37 at tds.net
Mon Aug 13 04:08:36 CEST 2007
bhaaluu wrote:
> Greetings,
>
>>From what I can tell of this "decoding" function, it uses
> the chr() function to return the ascii character:
>
>>>> print chr(eval('65'))
> A
There is no need to use eval() here. Since the expected values are
integers, just use int():
In [6]: chr(int('65'))
Out[6]: 'A'
This gives a clearer error message when the input is not as expected:
In [7]: chr(int('How'))
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.ValueError'>: invalid literal for int() with base 10:
'How'
In general it's a good idea to avoid using eval() especially with user
input, it is a gaping security hole.
Kent
More information about the Tutor
mailing list