[Tutor] A slight bug in IDLE
Dave Angel
davea at davea.name
Mon Jul 15 01:00:57 CEST 2013
On 07/14/2013 03:28 PM, Jim Mooney wrote:
> On 13 July 2013 22:25, Dave Angel <davea at davea.name> wrote:
>
> try:
>> input = raw_input
>> except NameError as e:
>> pass
>>
>> try:
>> range = xrange
>> except NameError as e:
>> pass
>>
>> ======
> Couldn't I just shorten that to:
>
> if int(sys.version[0]) < 3:
> try:
> input = raw_input
> range = xrange
> except NameError as err:
> pass
>
> Since it's the same error in both cases, and I only pass in either case, so
> the logic looks the same to me? Or am I missing something?
>
With enough research, you can justify any of those three approaches.
But there's a principle here. Instead of explicitly coding when a
particular change happened, and hardwiring the version number, one
should test the actual property whenever possible. I'm doing that check
with the try/catch. And by having two separate try/catch phrases, we
won't be making any assumptions about whether the two changes happened
at the same moment in revision history.
It's analogous to duck-typing. Instead of thinking you know which types
are acceptable, just check whether the methods you need are all
available. If they are, then the type is probably just fine. And in
this case, we don't usually need to check it all up front, but can just
use the methods as needed.
--
DaveA
More information about the Tutor
mailing list