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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:18:42+00:00 2026-05-24T14:18:42+00:00

I wrote a test program that looked like this: #!/usr/bin/python def incrementc(): c =

  • 0

I wrote a test program that looked like this:

#!/usr/bin/python

def incrementc():
    c = c + 1

def main():
    c = 5
    incrementc()

main()

print c

I’d think that since I called incrementc within the body of main, all variables from main would pass to incrementc. But when I run this program I get

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    main()
  File "test.py", line 8, in main
    incrementc()
  File "test.py", line 4, in incrementc
    c = c + 1
UnboundLocalError: local variable 'c' referenced before assignment

Why isn’t c passing through? And if I want a variable to be referenced by multiple functions, do I have to declare it globally? I read somewhere that global variables are bad.

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-05-24T14:18:43+00:00Added an answer on May 24, 2026 at 2:18 pm

    You’re thinking of dynamic scoping. The problem with dynamic scoping is that the behavior of incrementc would depend on previous function calls, which makes it very difficult to reason about the code. Instead most programming languages (also Python) use static scoping: c is visible only within main.

    To accomplish what you want, you’d either use a global variable, or, better, pass c as a parameter. Now, because the primitives in Python are immutable, passing an integer can’t be changed (it’s effectively passed by value), so you’d have to pack it into a container, like a list. Like this:

    def increment(l):
        l[0] = l[0] + 1
    
    def main():
        c = [5]
        increment(c)
        print c[0]
    
    main()
    

    Or, even simpler:

    def increment(l):
        return l + 1
    
    def main():
        c = 5
        print increment(c)
    
    main()
    

    Generally, global variables are bad because they make it very easy to write code that’s hard to understand. If you only have these two functions, you can go ahead and make c global because it’s still obvious what the code does. If you have more code, it’s better to pass the variables as a parameter instead; this way you can more easily see who depends on the global variable.

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

Sidebar

Related Questions

I wrote a test program like this: #include <sys/socket.h> int main( void ) {
I wrote a test program thinking that the address of p1 will be less
I need to test a program I wrote that receives XML as a text
I have a test program that I wrote to try and debug a GMutex
I wanted to write a program that test if two files are duplicates (have
I wrote a test program to monitor my Picture folder which points to c:\users[username]\Pictures
I wrote a small program to test accessing a widget parent's slot. Basically, it
I wrote an XML RPC server in python and a simple Test Client for
I wrote a small program that uses Mechanize to traverse a site. I want
I wrote a test program for testing Cassandra, and I had problems reading data.

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.