Correct usage instructions based on python goto

  • 2021-09-16 07:40:44
  • OfStack

The first is to install, python does not have this, so you need to install something else


pip install goto-statement

After reading an article, it is a direct official website, which can't be used at all, so I collected information and introduced it next

These 3 packages are to be used, not only the last one


import goto
from dominate.tags import label
from goto import with_goto

This must have. If there are multiple functions, an error will be reported. It should be put on the function to be used, for example


@with_goto
def xxx (): 
 xxx
 

My usage


def test(data):
  label.begin
  try:
   xxx
  except:
    goto.begin

Try to execute the program to be executed, and go back to the place where label. begin if there is an error

goto is good, but if you use it too much, it will lead to program confusion, so you should use it carefully

Supplement: python3 goto jumps to the specified line of code and executes the code

1. Requirement background:

After executing a certain step, it is found that the result is not in the desired form. If you want this loop to be executed again, you need to jump to a fixed position.

2. Using goto:

(1) Installing goto


pip install goto-statement

(2) Use goto to complete a small example

For official documents, see: https://pypi.org/project/goto-statement/

Note: If you run label and goto on ide Hill, there is a red wavy line indicating an error. Just execute it directly without paying attention


from goto import with_goto
 
@with_goto   # There must be 
def te(list_):
  tmp_list = list_
  label.begin    # Identify where to jump and start execution 
  result = []
  try:
    for i, j in enumerate(list_):
      tmp = 1 / j
      result.append(tmp)
      last_right_i = i
      if i == 1:
        print('----hhhhhhh')
        goto.begin
  except ZeroDivisionError:
    del tmp_list[last_right_i + 1]
    goto.begin   # Start execution where there is a jump flag 
  return result
 
if __name__ == '__main__':
  a = te([1, 3, 4, 0, 6])
  print(a)

Related articles: