Scope - import and globals

H

HMS Surprise

In the file snippet below the value for the global hostName is
determined at runtime. Functions imported from the parent baseClass
file such as logon also need access to this variable but cannot see it
the with the implementation I have attempted here.

Also, functions in this file and in the imported parent class need
PyHttpTestCase. Does there need to be an import statement in both
files?


Thanks,

jh

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


from PyHttpTestCase import PyHttpTestCase
from baseClass import baseClass

# definition of test class
class temp(baseClass):

def runTest(self):
global hostName
hostName = getHostNameFromUser()
self.logon()
 
T

Troels Thomsen

HMS Surprise said:
In the file snippet below the value for the global hostName is
determined at runtime. Functions imported from the parent baseClass
file such as logon also need access to this variable but cannot see it
the with the implementation I have attempted here.

Also, functions in this file and in the imported parent class need
PyHttpTestCase. Does there need to be an import statement in both
files?

If a file needs an import, make the import!
Dont rely on other file's imports , in general, imho.

Not sure excactly what you are doing, but are you sure you dont want an
instance variable instead of a global ?
self.hostName = "blah"
And then a getHostName() method in the class ?

regards
Troels
 
T

Tijs

HMS said:
In the file snippet below the value for the global hostName is
determined at runtime. Functions imported from the parent baseClass
file such as logon also need access to this variable but cannot see it
the with the implementation I have attempted here.

Use a class variable:

class baseClass:
hostName = None # undefined yet

def someFunc(self):
assert self.hostName is not None, "hostname not set yet"
... # use hostName here

class temp(baseClass):
def runTest(self):
baseClass.hostName = getHostName()
...

or a global variable:

baseClass.py:

hostName = None
class baseClass:
def someFunc(self):
assert hostName is not None
....

testme.py:

import baseClass
class temp(baseClass.baseClass):
....
baseClass.hostName = getHostName()

although neither solution strikes me as very elegant. I would normally pass
the hostname to the constructor of baseClass or use a separate 'settings'
module.

Global variables are per-module. Use the "global" keyword when assigning a
global variable in the 'current' module. Global variables of other modules
are properties of the module said:
Also, functions in this file and in the imported parent class need
PyHttpTestCase. Does there need to be an import statement in both
files?

Yes. Don't worry, the work is done only once.

Regards,
Tijs
 
D

Dennis Lee Bieber

Global variables are per-module. Use the "global" keyword when assigning a
global variable in the 'current' module. Global variables of other modules
are properties of the module, use <module>.<name>.
Having seen way too much code where the "global" was at module
level, I'd modify this to read:

Use the "global" keyword INSIDE THE FUNCTION DEFINITION when assigning
to a variable in the 'current' module.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top