468,301 Members | 1,477 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

Scope - import and globals


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()

May 29 '07 #1
2 1329

"HMS Surprise" <jo**@datavoiceint.comskrev i en meddelelse
news:11**********************@p47g2000hsd.googlegr oups.com...
>
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

May 29 '07 #2
HMS Surprise wrote:
>
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, use <module>.<name>.
>
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
May 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by lawrence | last post: by
reply views Thread by John Roth | last post: by
6 posts views Thread by Andy Baker | last post: by
4 posts views Thread by Hugh Macdonald | last post: by
10 posts views Thread by m.epper | last post: by
5 posts views Thread by Sandra-24 | last post: by
9 posts views Thread by bmaron2 | last post: by
3 posts views Thread by John Dann | last post: by
9 posts views Thread by Pat | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.