1

I have 3 files.

globalVar.py

global m_pTest
m_pTest = None

FileOne.py

import globalVar
import fileTwo
m_pTest = Class.getOutput() # Value is assigne
fileTwo.fun()
#Do anything

fileTwo.py

 import globalVar
 def fun():
 intVal = m_pTest.getInt() # int value from m_pTest object
 print intVal

This is my short program sample when i run this it gives error in fileTwo.py in fun()

 AttributeError: 'NoneType' object has no attribute 'getInt'

Can someone explain what is wrong in this code ? Thank you!

asked May 22, 2015 at 8:43
5
  • possible duplicate of Python global variable insanity Commented May 22, 2015 at 8:59
  • @brunodesthuilliers is there any option to avoid global i am using so many global variables Commented May 22, 2015 at 9:04
  • 1
    This isn't your actual code right? Running it will 1) get an error due to Class not being defined, and 2) even when I create my own object for Class, fun throws a NameError not an AttributeError. Commented May 22, 2015 at 9:21
  • 1
    @DigviJayPatil globals (module globals that is) are fine as long as they are considered as (pseudo) constants. Now when you have to share variables between functions / methods, the usual solutions are 1. using classes (so the methods can share the instance's state), 2. passing variables to functions/methods, 3. returning values from functions / methods. That's nothing new, really... Commented May 22, 2015 at 9:26
  • @brunodesthuilliers Can you give me some example it above problem context so that i can accept your answer Commented May 22, 2015 at 9:30

2 Answers 2

6

global m_pTest doesn’t make a variable magically global; instead it sets the name m_pTest in the current scope to refer to a global variable (from outer scope). So placing it in globalVar.py does basically nothing.

If you wanted to import only the variable, you could use the following:

from globalVar import m_pTest

However, when setting m_pTest to a different value, you will not affect the original object that was created in globalVar.py so the change isn’t seen elsewhere.

Instead, you have to import globalVar as normal, and then refer to m_pTest as a member of that module:

import globalVar
# reading
print(globalVar.m_pTest)
# setting
globalVar.m_pTest = 123
answered May 22, 2015 at 9:00
Sign up to request clarification or add additional context in comments.

1 Comment

Then how could i get value in fileTwo.py those are set in fileOne.py
4

Why using a global at all ?

# FileOne.py
import fileTwo
value = Class.getOutput() 
fileTwo.fun(value)
#Do anything
# fileTwo.py
def fun(value):
 intVal = value.getInt() 
 print intVal
answered May 22, 2015 at 9:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.