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 8110775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:52:33+00:00 2026-06-06T01:52:33+00:00

Very simple question: Specifically in Python (since Python actually has strongly recommended style guidelines

  • 0

Very simple question:

Specifically in Python (since Python actually has “strongly recommended” style guidelines specified in PEP 8, but really this applies to any language), should a function with an if clause that always returns have the alternative code in an else clause or not? In other words, func_style_one() and func_style_two() in the following piece of code are (obviously) exactly equivalent:

def func_style_one():
    if some_conditional_function():
        do_something()
        return something()
    else:
        do_something_else()
        return something_else()

def func_style_two():
    if some_conditional_function():
        do_something()
        return something()
    do_something_else()
    return something_else()

Obviously, the best and most readable style depends on the situation, and opinions will vary greatly on which is better, but I’m asking which is specifically preferred by the core Python community. (e.g. Which is used more often in the standard library, all other things being equal?)

  • 1 1 Answer
  • 0 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-06-06T01:52:37+00:00Added an answer on June 6, 2026 at 1:52 am

    As a rule of thumb, you should always avoid adding unneccessary complexity to your code, whatever the language. It also often is a good idea to try to split your code into semantically meaninful subsections.

    Given these heuristics, there is no definitive answer. It really boils down to what you are trying to achieve.

    I’ll demonstrate this with examples.

    If we have a function that checks for various error conditions before proceeding, it could make sense to write it without else:

    def do_stuff():
        if error1():
            return cleanup_and_fail()
        return ok()
    

    This is better as you often end up checking several errors in similar fashion in a sequence:

    def do_stuff():
        if error1():
            return cleanup_and_fail()
        if error2():
            return do_different_cleanup_and_fail()
        return ok()
    

    However, if your function instead branches to two equal branches, it could semantically make more sense to you else:

    def do_stuff():
        if option1():
            return do_option1()
        else:
            return do_option2()
    

    This is because you often end up adding several other options with elif:

    def do_stuff():
        if option1():
            return do_option1()
        elif:
            return do_option2()
        else:
            return do_option3()
    

    To summarize: think about the semantics of your code and choose syntax accordingly.

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

Sidebar

Related Questions

Very simple question, but I want to start using a consistent naming convention for
This is a very simple question, but I can't seem to find something about
this may be a very simple question, but is it possible to force a
This is a very simple question and I feel stupid for asking it, but
This may be very simple question,But please help me. i wanted to know what
Very simple question but giving me hard time, I want to replace to \
I'm sorry, I know this is a very simple question but I've search here
a very simple question I am afraid but I have been stuck for days
Very simple question, but nonetheless confusing. According to the Microsoft documentation, it is necessary
very simple question but I couldn't find an answer for this: I have the

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.