Why Python allows comparison of a callable and a number?

Discussion in 'Python' started by 一首诗, Nov 22, 2009.

  1. 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?
     
    一首诗, Nov 22, 2009
    #1
    1. Advertising

  2. 一首诗

    Chris Rebert Guest

    On Sun, Nov 22, 2009 at 4:03 AM, 一首诗 <> 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
     
    Chris Rebert, Nov 22, 2009
    #2
    1. Advertising

  3. 一首诗

    MRAB Guest

    一首诗 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. :)
     
    MRAB, Nov 22, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,071
    Smokey Grindel
    Dec 2, 2006
  2. Lighter
    Replies:
    10
    Views:
    503
    Vikram
    Jul 10, 2006
  3. exiquio
    Replies:
    2
    Views:
    550
    exiquio
    Oct 7, 2008
  4. Deepu
    Replies:
    1
    Views:
    249
    ccc31807
    Feb 7, 2011
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    119
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page