Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 491595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:01:38+00:00 2026-05-13T02:01:38+00:00

Is there a rationale to decide which one of try or if constructs to

  • 0

Is there a rationale to decide which one of try or if constructs to use, when testing variable to have a value?

For example, there is a function that returns either a list or doesn’t return a value. I want to check result before processing it. Which of the following would be more preferable and why?

result = function();
if (result):
    for r in result:
        #process items

or

result = function();
try:
    for r in result:
        # Process items
except TypeError:
    pass;

Related discussion:

Checking for member existence in Python

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T02:01:38+00:00Added an answer on May 13, 2026 at 2:01 am

    You often hear that Python encourages EAFP style (“it’s easier to ask for forgiveness than permission”) over LBYL style (“look before you leap”). To me, it’s a matter of efficiency and readability.

    In your example (say that instead of returning a list or an empty string, the function were to return a list or None), if you expect that 99 % of the time result will actually contain something iterable, I’d use the try/except approach. It will be faster if exceptions really are exceptional. If result is None more than 50 % of the time, then using if is probably better.

    To support this with a few measurements:

    >>> import timeit
    >>> timeit.timeit(setup="a=1;b=1", stmt="a/b") # no error checking
    0.06379691968322732
    >>> timeit.timeit(setup="a=1;b=1", stmt="try:\n a/b\nexcept ZeroDivisionError:\n pass")
    0.0829463709378615
    >>> timeit.timeit(setup="a=1;b=0", stmt="try:\n a/b\nexcept ZeroDivisionError:\n pass")
    0.5070195056614466
    >>> timeit.timeit(setup="a=1;b=1", stmt="if b!=0:\n a/b")
    0.11940114974277094
    >>> timeit.timeit(setup="a=1;b=0", stmt="if b!=0:\n a/b")
    0.051202772912802175
    

    So, whereas an if statement always costs you, it’s nearly free to set up a try/except block. But when an Exception actually occurs, the cost is much higher.

    Moral:

    • It’s perfectly OK (and “pythonic”) to use try/except for flow control,
    • but it makes sense most when Exceptions are actually exceptional.

    From the Python docs:

    EAFP

    Easier to ask for forgiveness than
    permission. This common Python coding
    style assumes the existence of valid
    keys or attributes and catches
    exceptions if the assumption proves
    false. This clean and fast style is
    characterized by the presence of many
    try and except statements. The
    technique contrasts with the LBYL
    style common to many other languages
    such as C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is it not advisable to use JavaScript in JSP? One rationale that I
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
Three of my coworkers just told me that there's no reason to use a
I find it strange that there is no built in function to just take
Background: There are numerous PHP addons out there that have some nice ideas, but
I have a very thin interface that's going to define one method. Should I
Rationale I try to avoid assignments in C++ code completely . That is, I
There is a moment in my app, that I need to force to show
There is a column that exists in 2 tables. In table 1, this column
There is a directed graph having a single designated node called root from which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.