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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:29:03+00:00 2026-06-13T11:29:03+00:00

Python scope I have the same question but slightly different. number = 0 def

  • 0

Python scope
I have the same question but slightly different.

number = 0
def incrementNumber():
    number += 1

This one above does not work but this one below does why? Both are outside the function scope.

number = {'num':0}
def incrementNumber():
    number['num'] += 1

First one works if I add the variable as global

number = 0
def incrementNumber():
    global number
    number += 1
  • 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-13T11:29:04+00:00Added an answer on June 13, 2026 at 11:29 am

    Check out this blog post, it’s similar to what you’re doing. Specifically adam’s comment.

    You don’t assign to dictionaryVar, you assign to dictionaryVar[‘A’].
    So it’s never being assigned to, so it’s implicitly global. If you
    were to actually assign to dictionaryVar, you’d get the behavior you
    were “expecting”.

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

Sidebar

Related Questions

I have one question, I have such code this._engine = Python.CreateEngine(); ScriptSource source =
This question is a follow-up on a question about Python variable scope . Additional
ORIGINAL QUESTION: (My question applies to Python 3.2+, but I doubt this has changed
If I have the following code class One: scope = 'first_scope' class Two: scope
Suppose I have the following python code: def outer(): string = def inner(): string
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
Is this possible in Python? class MyClass(object): @property def property(self): return self._property That is,
Say, I have some scope with variables, and a function called in this scope
How do you explicitly access name in Python's built in scope? One situation where
This is more of a theoretical question than anything. I have 3 dictionaries that

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.