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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:37:58+00:00 2026-05-15T11:37:58+00:00

This wants me to dig deeper in Python sources, but since there are many

  • 0

This wants me to dig deeper in Python sources, but since there are many people on SO that already done that, I would love to hear their pointers.

>>> import os
>>> def scope():
...     print os
...     import os
... 
>>> scope()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in scope
UnboundLocalError: local variable 'os' referenced before assignment

It seems to me that when parser interprets the file, it automatically creates local scope for scope function, that makes os “detached” from global scope.

Is it true? Anyone care to point me where I can find more about scope implementation?

EDIT: Also, this is not special case of imports, this works for usual variables too.

  • 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-15T11:37:58+00:00Added an answer on May 15, 2026 at 11:37 am

    When you call scope() Python sees that you have a local variable called os used inside your method (from the import inside scope) so this masks the global os. However when you say print os you haven’t reached the line and executed the local import yet so you see the error regarding reference before assignment. Here are a couple of other examples that might help:

    >>> x = 3
    >>> def printx():
    ...     print x # will print the global x
    ...
    >>> def printx2():
    ...     print x # will try to print the local x
    ...     x = 4
    ...
    >>> printx()
    3
    >>> printx2()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in printx2
    UnboundLocalError: local variable 'x' referenced before assignment
    

    And going back to your os example. Any assignment to os has the same effect:

    >>> os
    <module 'os' from 'C:\CDL_INSTALL\install\Python26\lib\os.pyc'>
    >>> def bad_os():
    ...     print os
    ...     os = "assigning a string to local os"
    ...
    >>> bad_os()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in bad_os
    UnboundLocalError: local variable 'os' referenced before assignment
    

    Finally, compare these 2 examples:

    >>> def example1():
    ...     print never_used # will be interpreted as a global
    ...
    >>> def example2():
    ...     print used_later # will be interpreted as the local assigned later
    ...     used_later = 42
    ...
    >>> example1()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in example1
    NameError: global name 'never_used' is not defined
    >>> example2()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in example2
    UnboundLocalError: local variable 'used_later' referenced before assignment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a silly question, but I'm working on a project that wants
In this question the OP implies that he wants to base the blog system
I'm trying to dig into jQuery but would like some feedback on how to
It hasn't been long since I started learning python, but I really want to
I know this sounds a little bizarre, but there is a very simple application
This is a project for a client and he wants it to use TouchesBegan/Moved/Ended
This could quite possibly be the dumbest question ever asked. Our client wants us
I'm working on this project and the customer wants it to integrate with a
is it just me or this code in Programming Pearls is wrong (quicksort wants
I don't have any idea how to use this class in .net. Anyone wants

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.