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

  • Home
  • SEARCH
  • 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 6921243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:15:05+00:00 2026-05-27T10:15:05+00:00

Due to the scoping rules of Python, all variables once initialized within a scope

  • 0

Due to the scoping rules of Python, all variables once initialized within a scope are available thereafter. Since conditionals do not introduce new scope, constructs in other languages (such as initializing a variable before that condition) aren’t necessarily needed. For example, we might have:

def foo(optionalvar = None):
    # some processing, resulting in...
    message = get_message()
    if optionalvar is not None:
        # some other processing, resulting in...
        message = get_other_message()
    # ... rest of function that uses message

or, we could have instead:

def foo(optionalvar = None):
    if optionalvar is None:
        # processing, resulting in...
        message = get_message()
    else:
        # other processing, resulting in...
        message = get_other_message()
    # ... rest of function that uses message

Of course, the get_message and get_other_message functions might be many lines of code and are basically irrelevant (you can assume that the state of the program after each path is the same); the goal here is making message ready for use beyond this section of the function.

I’ve seen the latter construct used several times in other questions, such as:

  • https://stackoverflow.com/a/6402327/18097
  • https://stackoverflow.com/a/7382688/18097

Which construct would be more acceptable?

  • 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-05-27T10:15:06+00:00Added an answer on May 27, 2026 at 10:15 am

    Python also has a very useful if syntax pattern which you can use here

      message = get_other_message() if optional_var else get_message()
    

    Or if you want to compare strictly with None

      message = get_other_message() if optional_var is not None else get_message()
    

    Unlike with example 1) you posted this doesn’t call get_message() unnecessarily.

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

Sidebar

Related Questions

This problem is partly due to my lack of completely understanding scoping in python,
This question is a follow-up on a question about Python variable scope . Additional
Due to the length of the code I can not post it here, I
Due to the upload script I can not obtain the uploaded file by $_FILE
Due to a security requirement all browsers that will run a web application we
Due to some bad practices regarding branching within a project in work, I am
Due to the broad scope of the original question, the question's scope has been
Due to some memory problems I had to textureBitmap.recycle() all texture bitmaps after I
Due to MSVC10 not allowing the use of inline ASM instructions when targeting x64
Due to repetitive errors with one of our Java applications: Engine engine_0: Error in

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.