#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Opposite of Continue?


    Is there anything that would function like the opposite of continue? Instead of continuing a task (preferrably a function), is there something that would work like a discontinue? So that it would do what VB6 calls Exit Sub. As of now I have to use boolean values to determine whether or not to continue. It's very annoying. :/
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can use the break keyword to exit from a loop; if you want to exit the program then I suggest raising SystemExit .

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    what netytan and if you are trying to leave a function. You can use
    return True
    ^^this will return True from the function coller. Or it can just exit if you have nothing picking up what was returned.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Or you could always raise an exception too . There's more than one way to skin a cat.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Originally Posted by netytan
    You can use the break keyword to exit from a loop; if you want to exit the program then I suggest raising SystemExit .

    Mark.
    I need to exit a function. I think I'll go with what Scorpions4Ever suggested, I didn't even think of that. Thanks guys.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Oy Vey. If you want to return out of a function, then why not just use the return keyword. It is a lot less heavyweight than an exception.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    What do you mean by return out of a function? That's making me think that I return something from the function, but meanwhile I have a bunch of other code for these functions that is also supposed to be run, unless an error occurs. This is why I need to stop the function in times of error. If return is the better way to go, how would I use it properly?
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    What do you mean when you say to use return? I'm thinking that this means to return something from the function, but meanwhile I have other code that has to be run, unless an error occurs, this is why I need something to end the function in times of error. If return is the best way to go in this case, how would I go about using it properly?
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Use the return keyword with no arguments.
    Code:
    def foo(x):
        if x < 5:
            return
        else:
            for i in range(x):
                 print i
        return x
    
    foo(4) # does nothing
    foo(8) # prints things.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, I'll try that. Thanks.
    PS. It had an error on my first post and I couldn't see it, therefore I posted twice.
    Update: I had some time to test it out in the interpreter. It works great, thanks.
    Last edited by †Yegg†; April 21st, 2005 at 08:56 PM.
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by †Yegg†
    What do you mean when you say to use return? I'm thinking that this means to return something from the function, but meanwhile I have other code that has to be run, unless an error occurs, this is why I need something to end the function in times of error. If return is the best way to go in this case, how would I go about using it properly?
    You should note that if an error occurs in your function, and the program itself will exit unless the exception is caught anyway so you don't need to catch and raise an exception yourself.

    Mark.
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    Originally Posted by †Yegg†
    Is there anything that would function like the opposite of continue? Instead of continuing a task (preferrably a function), is there something that would work like a discontinue? So that it would do what VB6 calls Exit Sub. As of now I have to use boolean values to determine whether or not to continue. It's very annoying. :/

    Your question isn't logical, you asked for the opposite of continue for functions,
    continue and break are opposite to each other and used only for loops while functions uses other methods


    so loops e.g (for, while) uses the following structure
    PHP Code:
    for i in range(10):
        if .....: continue 
    #This will continue to next i or until the loop stops, 
                              #tasks under this won't be excuted in this case
        #More Tasks
        
    if .....: break #This will exit the loop completely 
    now continue isn't logical in a function because there's nothing to repeat, you can instead manipulate the function structure by branching e.g (using if elif else) and specify what to return ( once returned function is exited )

    exceptions gives you good options for branching instead of if

    let's consider a function for converting any non-base10 number to base10
    PHP Code:
    def Any2Base10(Arg,intBase):
        
    ExMap "0123456789abcdefghijklmnopqrstuvwxyz"
        
    strArg=str(Arg)[::-1]
        
    intResult=0
        
    for k in xrangelen(strArg) ):
            
    Base10 ExMap.findstrArg[k].lower() )
            if 
    Base10>=intBaseraise "Invalid Base"
                             
    if Base10<0raise "Invalid Char"
            
    intResult+= (intBase** ) * Base10
        
    return intResult 
    now let's say you encountered many errors in the above code, while you might say return (None) or 0, but at different points you must know where did you reached so suggesting returning different values and so on won't fix the problem, maybe returning a string for error, but yet this is a weak approach, or actually this is what raise actually do, it stops the function, and returns the error name or object it's up to you, and all you got to do is catch it with exception, raise gives you the option to go throw the code and tells you where the error occured, and using except you need not to worry but it's more like a more powerful if else

    so here's how we call the function
    PHP Code:
    try:
          print 
    Any2Base10("65fa21b",16)
    except "Invalid Char"#The Name of the error string we raised
          
    print "You have supplied an invalid characted"
    except "Invalid Base":
          print 
    "Characted exceeded the Base limit, please revise"
    except:
          print 
    "An Unexpected error occured"

    #Try this with the following
    #print Any2Base10("10010",2)
    #print Any2Base10("321456",8)
    #print Any2Base10("102",2) 
    exceptions are extended not only using "string" but you can subclass an exception and create a whole new exception object, to use globally in all functions you need, and you make sure that it isn't confused with other exceptions

IMN logo majestic logo threadwatch logo seochat tools logo