Is there a label goto in Python

0 votes
is there a goto or any equivalent in Python to jump to a specific line of code?
Feb 3 in Python by Dev
• 6,000 points
36 views

1 answer to this question.

0 votes

Since Python is a structured programming language, it does not have a label or goto but by creating a function one can use goto in Python. In the official design Python History there is a mention to why there is no goto?
Exceptions can be used to provide a "structured goto" that works across function calls. 
Many people believe that exceptions can easily mimic all sensible uses of C, Fortran, and other languages' "go" and "goto" features

class label(Exception): pass  # declare a label

try:
    ...
    if condition: raise label()  # goto label
    ...
except label:  # where to goto
    pass
... 

You can't leap into the middle of a loop with this, but that's normally considered a goto misuse anyway. It is advised to use this function wisely and rarely.
Hope this helps!

answered Feb 4 by Nandini
• 5,480 points

Related Questions In Python

0 votes
1 answer

Is there a label/goto in Python?

No, Python does not support labels and ...READ MORE

answered Aug 1, 2018 in Python by Priyaj
• 58,080 points
366 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
431 views
0 votes
1 answer

Is there a string 'contains' in python?

In this case, you can use the ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 13,300 points
308 views
0 votes
1 answer

Is there a stack/heap in Python?

Memory management in Python involves a private ...READ MORE

answered Nov 9, 2018 in Python by SDeb
• 13,300 points
1,161 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,007 views
0 votes
1 answer
0 votes
1 answer

Is there a way to create multiline comments in Python?

In Python, you can use '''  some ...READ MORE

answered Feb 7 in Python by Nandini
• 5,480 points
44 views
0 votes
1 answer

What is the use of "assert" in Python?

The statement assert exists in almost every programming ...READ MORE

answered Feb 7 in Python by Nandini
• 5,480 points
49 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP