April 21st, 2005, 04:04 PM
-
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. :/
April 21st, 2005, 04:43 PM
-
You can use the break keyword to exit from a loop; if you want to exit the program then I suggest raising SystemExit
.
Mark.
April 21st, 2005, 04:58 PM
-
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.
April 21st, 2005, 07:56 PM
-
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
April 21st, 2005, 08:23 PM
-
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.
April 21st, 2005, 08:27 PM
-
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
April 21st, 2005, 08:31 PM
-
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?
April 21st, 2005, 08:35 PM
-
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?
April 21st, 2005, 08:35 PM
-
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
April 21st, 2005, 08:36 PM
-
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.
April 23rd, 2005, 12:00 PM
-
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.
April 23rd, 2005, 11:36 PM
-
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 xrange( len(strArg) ):
Base10 = ExMap.find( strArg[k].lower() )
if Base10>=intBase: raise "Invalid Base"
if Base10<0: raise "Invalid Char"
intResult+= (intBase** k ) * 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