Why Python allows comparison of a callable and a number?

Chris Rebert clp2 at rebertia.com
Sun Nov 22 13:09:08 CET 2009


On Sun, Nov 22, 2009 at 4:03 AM, 一首诗 <newptcai at gmail.com> wrote:
> I used python to write an assignment last week, here is a code snippet
>
> #================================
>
> def departTime():
>    '''
>    Calculate the time to depart a packet.
>    '''
>    if(random.random < 0.8):
>        t = random.expovariate(1.0 / 2.5)
>    else:
>        t = random.expovariate(1.0 / 10.5)
>    return t
>
> #================================
>
> Can you see the problem?  I compare random.random with 0.8,  which
> should be random.random().
>
> Of course this because of my careless, but I don't get it.  In my
> opinion, this kind of comparison should invoke a least a warning in
> any programming language.
>
> So why does python just ignore it?

It's an historical anomaly that's been rectified in Python 3, where
such non-equality comparisons between unrelated types *do* now raise
an error.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list