Results 1 to 5 of 5

Thread: Python, why use <= instead of == ?

  1. #1
    Join Date
    Dec 2006
    Location
    Athens, Georgia.
    Beans
    151
    Distro
    Ubuntu 8.04 Hardy Heron

    Python, why use <= instead of == ?

    This question is a very basic python question. I'm trying to teach myself Python using the "Think Python" book and I've made it to conditional statements. All the examples in the book use <= in places I wanted to use ==. What is the reason for that? Below is a copy/paste of one of the sample conditional statements. I wanted to use if n == 0, and I don't quite understand the need to include the negative numbers below 0.

    Code:
    def print_n(s, n):
        if n <= 0:
            return
        print s
        print_n(s, n-1)

  2. #2
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python, why use <= instead of == ?

    If you use "n==0" and someone were to be so unwise as to run
    Code:
    print_n('Hi',-1)
    then they fall into an infinite loop.

  3. #3
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: Python, why use <= instead of == ?

    I'm not a Python guru, so I'll approach an answer more from a "general know-how" sense.

    In short, you need to make sure that your function will stop when it should.

    If you're throwing together code for a function for someone else to use, you can't guarantee that they'll provide valid input: if they give a function for, say, factorials a value where n<0, then not only will it not make sense, you could end up with a horibble mess. If you're lucky, it will result in an exception that your error handling code will catch.
    Last edited by lisati; July 24th, 2009 at 05:05 AM. Reason: Minor rewording.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python, why use <= instead of == ?

    Quote Originally Posted by unutbu View Post
    If you use "n==0" and someone were to be so unwise as to run
    Code:
    print_n('Hi',-1)
    then they fall into an infinite loop.
    And not only someone will consciously do that, it might happen at random, say:

    PHP Code:
    10
    5

    # ... code here ...

    += 6
    print_n
    ('Hello'y
    Thus, the problem becomes transparent to the person (including yourself) using the function. The thing is, functions are enclosed objects that should behave as expected, therefore, I don't expect that when calling a function made by other person (say you created a library and I'm using it) it will go on a infinite loop and crash my program.

    The "pythonic" (well, object-oriented) way to handle it is by throwing the proper error:

    PHP Code:
    def recursion(sn):
        
        if 
    == 0:
            return 
    None
        elif n 
    0:
            
    raise ValueError("Argument 'n' must be positive.")
        
        print 
    s
        recursion
    (sn-1
    But exception Handling is not something I would mess with at this point, so leave that for later.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  5. #5
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python, why use <= instead of == ?

    Quote Originally Posted by Can+~ View Post
    The "pythonic" (well, object-oriented) way to handle it is by throwing the proper error:

    PHP Code:
    def recursion(sn):
        
        if 
    == 0:
            return 
    None
        elif n 
    0:
            
    raise ValueError("Argument 'n' must be positive.")
        
        print 
    s
        recursion
    (sn-1
    But exception Handling is not something I would mess with at this point, so leave that for later.
    EDIT: nvr mind. doh!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •