How Best to Coerce Python Objects to Integers?
Erik
python at lucidity.plus.com
Tue Jan 3 21:09:40 EST 2017
On 04/01/17 01:10, Steve D'Aprano wrote:
> On Wed, 4 Jan 2017 11:22 am, Erik wrote:
>> What he *should* have done is just validated his input strings before
>> presenting the string to int() - i.e., process the input with knowledge
>> that is specific to the problem domain before calling the
>> general-purpose function.
>
> That's the Look Before You Leap solution. But in this case, given the
> scenario described (a text file with a few typos), the best way is to ask
> for forgiveness rather than permission:
Yes, probably, in this case ;)
OK, in the case where the function you're calling is sane (and Python's
int() is - it won't blindly accept "0x101" as a hex value, for example)
then it's probably right that leaping first and then, on failure,
processing the value and leaping again is the right thing to do.
[I tend to work in an environment where things I'm calling may not be
sane (and in some cases I may never know), so I will usually consider
LBYL as a way of CMA ;)].
In this whole discussion there has been no mention of what happens when
the function returns None, though.
> Another thought: if he is receiving human generated input, there is an
> argument to be made for accepting "look alikes" -- e.g. maybe the data was
> entered by Aunt Tilly, who was a typist in the 1960s and can't break the
> habit of using l or I interchangeably for 1, and O for 0.
Sure - and that's what I meant by processing the string according to his
problem "domain". If he has Aunt Tillys doing his data input, then l->1
and 0->O may be a reasonable thing (I recently did a project where
things like converting Icelandic's Eth and Thorn runic letters to 'D'
and 'P' - though morally wrong ;) - was a reasonable character
translation because that's what people actually typed in).
E.
More information about the Python-list
mailing list