PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 25, 2022

[FIXED] Why global value doesn't change when reading from another module?

 November 25, 2022     global, module, python     No comments   

Issue

I have three files:

globals.py:

value = None

reader.py:

from globals import *

def read_global():
    print(value)

changer.py:

from globals import *
from reader import *

def change_global():
    global value
    value = 1


change_global()
read_global()

I would expect the call to "read_global" would print 1, but the value None is printed.

Why is that the case? Why the new value set in "change_global" doesn't print?


Solution

I've been reading about modules in the Python documentation and it states:

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.

This implicates that the global symbol table is not shared between modules.



Answered By - luke1985
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing