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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:38:02+00:00 2026-05-30T07:38:02+00:00

Here is a python module, #a.py def bar(): print x #x not defined, apparently

  • 0

Here is a python module,

#a.py

def bar():
    print x  #x not defined, apparently will result in an error

def foo():
    pass

if __name__ == '__main__':
    foo()

The above module can be run ($ python a.py) without any error. Why? Just because bar is not used in __main__?

But bar‘s definition is executed, isn’t it?

  • 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-30T07:38:03+00:00Added an answer on May 30, 2026 at 7:38 am

    Yes, bar‘s definition is executed, but the definition doesn’t contain an error. It is valid Python to define a function that refers to globals that don’t yet exist, so long as they exist when the function is called. Consider this:

    def bar():
        print x
    
    x = 10
    if __name__ == '__main__':
        bar()
    

    This does not result in an error. And this is only sensible, since even if x exists at the time the function is defined, there is nothing to stop you using del on it later. The point when x needs to be defined is when bar is called (if ever), not when bar is defined.

    If Python did work the way you are suggesting, then it would be impossible to define mutually recursive functions without weird hacks like temporarily binding one name to None, then defining both functions.

    EDIT: To elaborate on Ignacio’s answer to the Alcott’s question in the comments, yes syntax errors are caught before the function can be executed, but they’re actually caught before it can be defined either.

    When Python loads a file, it parses the entire contents into statements and then executes the statements one at a time. A syntax error means it was unable to successfully figure out what statements the file contains, so it can’t execute anything. So the error will occur when the file is loaded, which means either when you directly run it with the interpreter, or when you import it.

    This pre-processing step is known as “compile time”, even though Python is not normally thought of as a compiled language; it is technically compiled to a byte code format, but this is almost entirely uninteresting because the byte code pretty much just directly represents the source code statements.

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

Sidebar

Related Questions

Here is a sample python module: # foo.py class Foo(object): a = {} def
python's time module seems a little haphazard. For example, here is a list of
I have a problem with the Python logging module, I'm not sure if I'm
I want to add a list of dicts together with python multiprocessing module. Here
I'm trying to use the multiprocessing module in python 2.6, but apparently there is
Here is a special Haskell program which outputs a Python program that outputs a
I'm curious how the Python Ninjas around here would do the following, elegantly and
In python, you can have a function return multiple values. Here's a contrived example:
Python, through it's readline bindings allows for great command-line autocompletion (as described in here
I'm building a python application from some source code I've found Here I've managed

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.