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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:00:28+00:00 2026-06-15T13:00:28+00:00

I created a simple decorator that received an argument (using functions instead of a

  • 0

I created a simple decorator that received an argument (using functions instead of a class), when something strage happened: adding a line a code breaks the execution of a previous line.

Here’s the code:

def my_decorator(sublabel):
    def duration_wrapper(f):

        print sublabel
        # Uncommenting this code will break the previous line - why?
        # if sublabel is None:
        #     sublabel = f.func_name

        def wrapped_function(*args, **kwargs):
            return f(*args, **kwargs)

        return wrapped_function

    return duration_wrapper

@my_decorator('me')
def myf(): pass

myf()

Uncommenting those lines of code cause this exception:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    @my_decorator('me')
  File "test.py", line 4, in duration_wrapper
    print sublabel
UnboundLocalError: local variable 'sublabel' referenced before assignment

Can anyone explain why uncommenting those 2 lines of code break 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-06-15T13:00:30+00:00Added an answer on June 15, 2026 at 1:00 pm

    Decorators are closures, and all labels from an enclosing scope referenced from an enclosed scope must remain static within the enclosed scope. If you want to have a variable from an enclosing scope be mutable from an enclosed scope, you need to wrap it in a mutable object, for example:

    def my_decorator(sublabel):
        sublabel = [sublabel]
        def duration_wrapper(f):
    
            print sublabel[0]
            if sublabel[0] is None:
                sublabel[0] = f.func_name
    
            def wrapped_function(*args, **kwargs):
                return f(*args, **kwargs)
    
            return wrapped_function
    
        return duration_wrapper
    

    As @Bakuriu notes in the comments, Python 3 introduces nonlocal to remove this restriction, and you could also make sublabel a global to get around the problem, but globals are usually a bad idea generally.

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

Sidebar

Related Questions

Could someone help me on this, I have created simple web services using axis2
I created a simple login system using sql It has 4 main components index
I created a simple hello-wordish code using hibernate sf = new Configuration().configure().buildSessionFactory(); System.out.println(sessionFactory is
I have a simple form and a view that are doing something as basic
I'd like to create a Python class decorator (*) that would be able to
I am trying to define simple getter/setter methods for a mixin class that I
I've created a simple Attribute: [AttributeUsage(AttributeTargets.Method)] public class InitAttribute : System.Attribute { public InitAttribute()
I've created a very simple CSS that styles two buttons. To the first has
I am using the decorator pattern and am decorating a class with a constructor
I have built an abstract class that is used to handle command line options

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.