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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:00:22+00:00 2026-06-16T05:00:22+00:00

For some reason, the Python-2.7 timeit function crashes in the following example: a,b =

  • 0

For some reason, the Python-2.7 timeit function crashes in the following example:

a,b = 0,0
timeit a=b                  # ok: 10000000 loops, best of 3: 50.9 ns per loop
timeit if a==a+b: pass      # ok:  1000000 loops, best of 3: 129 ns per loop
timeit a=a+b                # crashes!

Traceback (most recent call last):
UnboundLocalError: local variable 'a' referenced before assignment

Apparently, I can assign to a (first example), I can compare a to a+b (2nd example), so why can’t I run the 3rd example ?!?! Of course, the statement being timed is, by itself, perfectly sound …

  • 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-16T05:00:24+00:00Added an answer on June 16, 2026 at 5:00 am

    timeit is actually a function but some python interpreters can allow you to use it with a statement‘s syntax, like IPython, But it is actually a function.

    So in a==a+b it actually considers a and b as global variable and therefore no Error, as it can fetch the global a and b.

    But in a=a+b it considers a as local variable and b is still global so there it raises the Error, because as soon as python sees assignment inside a function it considers it as a local variable.

    it is equivalent to:

    In [7]: def func1():
        a==a+b
       ...:     
    
    In [8]: def func():
        a=a+b
       ...:     
    
    In [9]: dis.dis(func1)
      2           0 LOAD_GLOBAL              0 (a)
                  3 LOAD_GLOBAL              0 (a)
                  6 LOAD_GLOBAL              1 (b)
                  9 BINARY_ADD          
                 10 COMPARE_OP               2 (==)
                 13 POP_TOP             
                 14 LOAD_CONST               0 (None)
                 17 RETURN_VALUE        
    
    In [10]: dis.dis(func)
      2           0 LOAD_FAST                0 (a)   # but there's nothing to load, so Error
                  3 LOAD_GLOBAL              0 (b)
                  6 BINARY_ADD          
                  7 STORE_FAST               0 (a)
                 10 LOAD_CONST               0 (None)
                 13 RETURN_VALUE        
    
    
    In [11]: func()     #same error as yours
    ---------------------------------------------------------------------------
    UnboundLocalError                         Traceback (most recent call last)
    
    UnboundLocalError: local variable 'a' referenced before assignment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to start a phantomjs process from python but for some reason it's
I'm trying to distribute a python module I wrote. For some reason, I can't
I'm trying to use Python to run pdftotext, but for some reason, my code
If anyone using WingIDE for their python dev needs: For some reason I get
My devserver has become hideously slow for some reason. (Python, Windows 7, GAE 1.3.3)
I wrote a CPU monitoring program in Python. For some reason sometimes the the
For some reason in Python 2.7 expressions of the form tuple > list return
I am using some python to do some variable name generation. For some reason
I'm having some trouble with Python's raw_input command (Python2.6), For some reason, the raw_input
For some reason I can't get python to find cairo. When I did ./waf

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.