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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:00:47+00:00 2026-06-11T01:00:47+00:00

I just learned python @ decorator, it’s cool, but soon I found my modified

  • 0

I just learned python @ decorator, it’s cool, but soon I found my modified code coming out weird problems.

def with_wrapper(param1):
    def dummy_wrapper(fn):
        print param1
        param1 = 'new'
        fn(param1)
    return dummy_wrapper

def dummy():
    @with_wrapper('param1')
    def implementation(param2):
        print param2

dummy()

I debug it, it throws out exception at print param1

UnboundLocalError: local variable 'param1' referenced before assignment

If I remove param1 = 'new' this line, without any modify operation (link to new object) on variables from outer scope, this routine might working.

Is it meaning I only have made one copy of outer scope variables, then make modification?


Thanks Delnan, it’s essential to closure.
Likely answer from here:
What limitations have closures in Python compared to language X closures?

Similar code as:

def e(a):
    def f():
        print a
        a = '1'
    f()
e('2')

And also this seems previous annoying global variable:

a = '1'
def b():
    #global a
    print a
    a = '2'
b()

This is fixed by add global symbol.
But for closure, no such symbol found.
Thanks unutbu, Python 3 gave us nonlocal.

I know from above directly accessing to outer variable is read-only.
but it’s kind of uncomfortable to see preceded reading variable(print var) is also affected.

  • 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-11T01:00:48+00:00Added an answer on June 11, 2026 at 1:00 am

    When Python parses a function, it notes whenever it finds a variable used on the left-hand side of an assignment, such as

    param1 = 'new'
    

    It assumes that all such variables are local to the function.
    So when you precede this assignment with

    print param1
    

    an error occurs because Python does not have a value for this local variable at the time the print statement is executed.


    In Python3 you can fix this by declaring that param1 is nonlocal:

    def with_wrapper(param1):
        def dummy_wrapper(fn):
            nonlocal param1
            print param1
            param1 = 'new'
            fn(param1)
        return dummy_wrapper
    

    In Python2 you have to resort to a trick, such as passing param1 inside a list (or some other mutable object):

    def with_wrapper(param1_list):
        def dummy_wrapper(fn):
            print param1_list[0]
            param1_list[0] = 'new'   # mutate the value inside the list
            fn(param1_list[0])
        return dummy_wrapper
    
    def dummy():
        @with_wrapper(['param1'])   # <--- Note we pass a list here
        def implementation(param2):
            print param2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started working in Python just recently and haven't fully learned all the nuts
I have just learned about recursion in Python and have completed assignments, one of
I've just learned the basics of Ruby after being very happy with Python for
I learned Python last year, and was just getting back into it with a
So I just learned about List Comprehensions in python. some of these are getting
I just saw a code snippet in Dive into Python where a function was
I just learned about ngrep , a cool program that lets you easily sniff
I just learned using MySQLDb package for python and I am familiar with fetching
Just learned Python 3 in 7 days, and I have the feeling that there's
I just learned how to use tkinter in Python (3.2.2), and I'm having some

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.