Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Why Python allows comparison of a callable and a number?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


newptcai at gmail

Nov 22, 2009, 4:03 AM

Post #1 of 3 (186 views)
Permalink
Why Python allows comparison of a callable and a number?

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?
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 22, 2009, 4:09 AM

Post #2 of 3 (184 views)
Permalink
Re: Why Python allows comparison of a callable and a number? [In reply to]

On Sun, Nov 22, 2009 at 4:03 AM, 一首诗 <newptcai [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Nov 22, 2009, 8:23 AM

Post #3 of 3 (174 views)
Permalink
Re: Why Python allows comparison of a callable and a number? [In reply to]

一首诗 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?

In Python 2 you can compare any 2 objects, for example an int with a
string. The result is arbitrary but consistent.

In Python 3 if the 2 objects aren't 'compatible' you'll get a TypeError
at runtime.

BTW, you don't need to put parentheses around the conditions in 'if' and
'while' statements. Python isn't C, etc. :-)
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.