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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:54:23+00:00 2026-05-22T01:54:23+00:00

I have a problem with my program and I was able to reproduce this

  • 0

I have a problem with my program and I was able to reproduce this unexpected (at least unexpected to me) behavior in a small scale so now I’m certain it is not another bug.

Lets say I have 3 python modules: one, two and three.

In three we have:

var = 0
list = []

So there we have a integer that is equal to zero and and empty list.

In two we have:

from three import var, list

def funct():
    print var*2
    print list
    return

So we import var and list and simply define a function that will print both and return.

Instead of calling funct() in two I called it in one, but not before doing some “operations” to them.

from three import var, list
from two import funct

if 2 < 4:
    var += 1
    list.append("x")

print funct()

So here comes my question.

I never expected this result:

0
['x']
None

How come the x was added to the list with the append() and 1 was NOT added to var, to be clear. I was expecting:

2
['x']
None

If find it very strange that they receive different treatments under the same circumstances.

  • Am I missing something here?
  • Am I doing something wrong with the imports?

If not:

  • Why does it behave like this?
  • How should this problem be solved / approached?

Thank in advance.

  • 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-22T01:54:24+00:00Added an answer on May 22, 2026 at 1:54 am

    To “fix” your program, you would have to add “global var”, as in:

    if 2 < 4:
         global var
         var += 1
         list.append("x")
    

    But this is illegal according to http://docs.python.org/reference/simple_stmts.html#global:

    Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

    Then again, following the link above:

    CPython implementation detail: The current implementation does not enforce the latter two restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program.

    Edit:

    The solution is to change:

    from three import var, list
    

    to:

    from three import list
    import three
    

    then don’t use global namespace, but specify explicitly the namespace of three (you imported the symbol)

    code:

    three.var += 1
    

    Make sure two.py and one.py are changed accordingly in their import/from statements

    http://docs.python.org/tutorial/modules.html#more-on-modules

    Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.

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

Sidebar

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
This is my problem. I have a program that has to run in a
I'm not sure what exactly to call this, but I've been able to reproduce
I have a problem with my WPF program. I'm trying to create an object
I have a problem I'm trying to solve involving interfaceing a C++ program with
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a problem running a simple Hello-world program in C++ on my Windows
I have a Java program which is being started via ProcessBuilder from another Java
I have problem with installing java JDK directly in C:. I was able to
An application in the field is getting this message intermittently: I am not able

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.