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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:20:40+00:00 2026-06-18T06:20:40+00:00

here is my code: def f(x): def g(n): if n < 10: x =

  • 0

here is my code:

def f(x):
    def g(n):
        if n < 10:
            x = x + 1
            g(n + 1)
    g(0)

When I evaluate f(0), there would be an error “x referenced before assignment”.

However, when I use “print x” instead of “x = x + 1” , it will work.

It seems that in the scope of g, I can only use x as an “use occurrence” but not a “binding occurrence”. I guess the problem is that f passes to g only the VALUE of x.

Am I understanding it correctly or not? If not, can someone explain why the left side of “x = x + 1” is not defined before reference?

Thanks

  • 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-18T06:20:41+00:00Added an answer on June 18, 2026 at 6:20 am

    You are understanding it correctly. You cannot use x to assign to in a nested scope in Python 2.

    In Python 3, you can still use it as a binding occurrence by marking the variable as nonlocal; this is a keyword introduced for just this usecase:

    def f(x):
        def g(n):
            nonlocal x
            if n < 10:
                x = x + 1
                g(n + 1)
        g(0)
    

    In python 2, you have a few work-arounds; using a mutable to avoid needing to bind it, or (ab)using a function property:

    def f(x):
        x = [x]   # lists are mutable
        def g(n):
            if n < 10:
                x[0] = x[0] + 1   # not assigning, but mutating (x.__setitem__(0, newvalue))
                g(n + 1)
        g(0)
    

    or

    def f(x):
        def g(n):
            if n < 10:
                g.x = g.x + 1
                g(n + 1)
        g.x = x  # attribute on the function!
        g(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: class MyTestCase(Base): def setUp(self): #some code here def test_B(self): #some
I'm new to Scala... Here's the code: def ack2(m: BigInt, n: BigInt): BigInt =
Here is some code: class Person def initialize(age) @age = age end def age
Here is the code for the relevant function: def populateSubscribers(csvfile, db): conn = sqlite3.connect(db)
Here is my code. @@inversions = 0 numbers = [very big array] def merge_sort(array)
Here's some example code: class Obj attr :c, true def == that p '=='
I am using this code taken from here : import smtplib def prompt(prompt): return
My code is based on the methods described here and here . def fraction?(number)
Ok the error is showing up somewhere in this here code if($error==false) { $query
I'm performing AJAX search in my Rails application. Here is code from controller: def

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.