Python Forum
right now a goto would be useable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
right now a goto would be useable
#1
i'm deep in a bunch of if clauses and a couple of for loops and my test detect a condition where i need to set up the dictionary to be returned then return. it's kind of like error handling or resource cleanup. there are too many variables it needs to make that setup code be another function. maybe i can wrap this in a big try/except.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Is there a question?
Reply
#3
Feature for Python 3.10: https://www.python.org/dev/peps/pep-0622/

Quote:Abstract

This PEP proposes to add a pattern matching statement to Python, inspired by similar syntax found in Scala and many other languages.

The pattern syntax builds on Python’s existing syntax for sequence unpacking (e.g., a, b = value), but is wrapped in a match statement which compares its subject to several different “shapes” until one is found that fits. In addition to specifying the shape of a sequence to be unpacked, patterns can also specify the shape to be a mapping with specific keys, an instance of a given class with (optionally) specific attributes, a specific value, or a wildcard. Patterns can be composed in several ways.

....
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
You haven't shown any code, but it sounds like the code is quite complicated. Perhaps it should be refactored to make it simpler to understand and change?
Reply
#5
yes, it is complicated. it has been refactored twice. it was a major disaster on the first coding. this section probably can be refactored better than before.

(Jul-09-2020, 07:16 AM)Gribouillis Wrote: Is there a question?

no. does there need to be to make a discussion in a discussion subforum?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Skaperen Wrote:does there need to be to make a discussion in a discussion subforum?
No but a piece of code would help otherwise we don't have much to discuss here.
Reply
#7
not that i am suggesting it but just saying i hit a case where i'd have coded a goto. it would be a downward goto, only. my rule about using them in C is downward only. that way simpler code analysis algorithms can still work. that was the original complaint about them in FORTRAN.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Here is an ersatz of downward goto
from collections import defaultdict

goto = label = defaultdict(
    lambda: type('Goto', (Exception,), {})).__getitem__

def main(x):
    try:
        ...
        if x == 3:
            raise goto('spam')
        ...
    except label('spam'):
        print('Goto worked!')

if __name__ == '__main__':
    main(3)
Reply
#9
A bit on a side, some interpreted languages have a break where you can specify the number of nested loops you want to break away, it helps sometime when you are to lazy to implement nice exception handling ....
Reply
#10
what exception would you suggest to use to do a downward goto that would not mess up the usual exception handling by the caller?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020