Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best pythonic one-time loop
#1
i use a one-time loop to surround code where i need a quick way out of a section of code. that way a break is somewhat like a goto end_of_block. the way i have been making a one-time loop is like for x in 'x': or other similar code. is this suitable or is there a better more pythonic way?
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
well, IMHO any code that requires goto is not pythonic and needs refactoring
Reply
#3
the other way to do it is a function. the return is the get out of here operation. loops already have break as a way to get out. but a if block does not. this is just making it possible to do break. this is not a goto.

python cannot do complex conditionals on one line (few languages can and the code ends up so ugly a multi-line way is preferred, especially if there are loops in the complex conditional).
Tradition is peer pressure from dead people

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pythonic from a C++ perspective PyDan 2 1,448 Sep-18-2023, 11:39 AM
Last Post: PyDan
  How to measure execution time of a multithread loop spacedog 2 4,005 Apr-24-2021, 07:52 AM
Last Post: spacedog
  how to create pythonic codes including for loop and if statement? aupres 1 2,443 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  Updating a matrix in a time interval inside a for loop vp1989 4 4,176 May-17-2020, 07:15 PM
Last Post: vp1989
  Is this use of exec pythonic? psolar 1 2,397 Feb-07-2020, 12:23 PM
Last Post: buran
  Loop independent of excecution time of a script Forelli 8 5,319 Feb-02-2020, 10:49 PM
Last Post: snippsat
  which is "better" (or more Pythonic)? Skaperen 2 2,862 Feb-01-2020, 03:10 PM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 7 4,567 Feb-01-2020, 03:51 AM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 8 4,935 Nov-16-2019, 06:46 PM
Last Post: Skaperen
  How to Display Multiple Time Tables With While Loop ZQ12 2 2,820 Nov-10-2019, 04:15 AM
Last Post: ZQ12

Forum Jump:

User Panel Messages

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