clix
Posts: 18
Joined: Fri Jun 27, 2014 4:51 pm

[Solved] Goto Decorator

Mon Aug 25, 2014 11:53 am

The goto decorator is a delicate subject, but basically I NEED it, or a very simple and easy workaround. I found this:
http://code.activestate.com/recipes/576 ... decorator/
but am unsure on how to use it, and it hasn't worked so far, returning

Code: Select all

NameError: name 'label' is not defined
to me using

Code: Select all

goto .done
to goto
and

Code: Select all

label .done
as the destination

I also tried that goto April Fools joke, but it didn't work either. Please excuse my lack of knowledge of Python and good coding methods, first language was C++ which has the goto statement. Only say "DON'T USE GOTO" if you're giving a replacement method.
Thanks
Clix
Last edited by clix on Tue Aug 26, 2014 9:44 pm, edited 1 time in total.

DirkS
Posts: 10362
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Goto Decorator

Mon Aug 25, 2014 1:10 pm

Only say "DON'T USE GOTO" if you're giving a replacement method.
To give an alternative we would need to know what you're trying to achieve.

Gr.
Dirk.

clix
Posts: 18
Joined: Fri Jun 27, 2014 4:51 pm

Re: Goto Decorator

Mon Aug 25, 2014 1:44 pm

Sorry, I forgot!
Very basically, I'm trying to achieve something like this:

1) Pick a random number
2) If the number is 1 got back to step 1, otherwise multiply it by 3
3) If it is now divisible by 4, go back to step 1, if the number is 6 skip straight to end, else divide by...
etc...
Basically performing a series of functions on a number but with a lot of conditions and jumping about (skip ahead to step x, g back to step x etc...)

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Goto Decorator

Mon Aug 25, 2014 2:09 pm

Do it all as functions.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Goto Decorator

Mon Aug 25, 2014 2:21 pm

The goto debate will never die.

FWIW, one of the basic theorems of the "structured programming" revolution is that anything you can do with "goto", you can do with one or more of the "structured" constructs. Unfortunately, this theorem falls apart if you add in the caveat that things like "break" and "continue" (and "return" in functions) are just "goto"s in disguise.

So, in shell, for example, when we need to code logic that says:

get input
check it
if bad display error and go back.

You have to code it as a while loop:

while :;do
get input
if good break
display error message
done

which is obviously silly, but that's the way it is.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

hippy
Posts: 7728
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Goto Decorator

Mon Aug 25, 2014 5:16 pm

clix wrote:1) Pick a random number
2) If the number is 1 got back to step 1, otherwise multiply it by 3
3) If it is now divisible by 4, go back to step 1, if the number is 6 skip straight to end, else divide by...
etc...
Basically performing a series of functions on a number but with a lot of conditions and jumping about (skip ahead to step x, g back to step x etc...)
It can usually be done and done elegantly without goto but without the full spec it's not possible to say how that would be. For your above example in non-Python pseudo code, something like ...

Code: Select all

Function GetRandomNumber
  Do
    Do
      n = Random()
    Loop Until ( n != 1 )
    n = n * 3
  Loop Until ( (n Mod 4) != 0 ) 
  If ( n != 6 ) Then
    ...
  End If
  Return n
End Function

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Goto Decorator

Mon Aug 25, 2014 5:37 pm

The reason Goto is deprecated is maintainability - when you're trying to work out what you did six months ago.
As you work your way through the code, and you follow a Goto, there's no easy way to find out where you came from. Functions etc. keep track of that for you.
But if you don't need to go back, if you don't care where you came from, why not use Goto?

clix
Posts: 18
Joined: Fri Jun 27, 2014 4:51 pm

Re: Goto Decorator

Tue Aug 26, 2014 11:20 am

Thanks for all the replies. Before I change the code a lot with the while loops and functions, any last clues on why the decorator isn't working and how I could get it to work? I'm running Python 2.7 and the decorator is pasted at the top of my code.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Goto Decorator

Tue Aug 26, 2014 11:37 am

The linked code does work. The assumption has to be you have copied it incorrectly or are not using it properly.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Goto Decorator

Tue Aug 26, 2014 1:30 pm

One of the comments on your link points out that the goto as described isn't fully fledged as it won't allow jumps into nested loops (at a different level of nesting from the jump point), maybe you've hit that (though doesn't look like if from your psuedocode)?

I still think it's worth thinking how you could do what you want without goto. Even if you end up with more lines of code it will be prettier.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

clix
Posts: 18
Joined: Fri Jun 27, 2014 4:51 pm

Re: Goto Decorator

Tue Aug 26, 2014 9:43 pm

paddyg wrote:One of the comments on your link points out that the goto as described isn't fully fledged as it won't allow jumps into nested loops
Missed that, thanks paddyg, that could be it. Thanks everyone, if I can't get goto to work I'll try to recode it with your suggestions.
Thanks again
Clix

Return to “Python”