By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

Problem in designing a global directory in python

P: n/a
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
--------------------
global sysctx

class Context:
def __init__(self):
def set(self, name, obj, overwrite=True):
def get(self, name):
def has(self, name):

def init():
global sysctx
sysctx = Context()

def getContext():
global sysctx
return sysctx
---------------------

init() is called somewhere at the beginning of the program.
In other modules, i want to use this in the following manner,

from utils import *
getContext().set(...)

but SOMETIMES I met following error located in "getContext()"
NameError: global name 'sysctx' is not defined

I found that when a module is in the same directory as utils.py, when I
can simply use "utils" for importing, there is no such problem. But
when i was writing a module in a deeper directory than utils.py, which
has to use the full module name for importing, such as:

from myproj.utils import *
getContext().set(...)

I got this error.

What should I do to correct that?

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Le 29 Mar 2005 09:50:46 -0800, Tian a écrit :
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
--------------------
global sysctx # you are in the global scope of the utils module. This "global sysctx"
# has no meaning, replace by
sysctx = None # create a global 'sysctx' name in utils namespace
class Context: class Context(object): # why not use "new-style" classes, we are in
2005 def __init__(self): # I suppose that there is some __doc__ and code :-) def set(self, name, obj, overwrite=True):
def get(self, name):
def has(self, name):

def init():
global sysctx
sysctx = Context()

def getContext():
global sysctx
return sysctx
---------------------

init() is called somewhere at the beginning of the program.
In other modules, i want to use this in the following manner,

from utils import * Please do not use the from module import * form
from utils import getContext getContext().set(...) You can also restrict the exported names of utils.py by adding a
__all__ = ('getContext',)
in utils.py.
but SOMETIMES I met following error located in "getContext()"
NameError: global name 'sysctx' is not defined

I found that when a module is in the same directory as utils.py, when I
can simply use "utils" for importing, there is no such problem. But
when i was writing a module in a deeper directory than utils.py, which
has to use the full module name for importing, such as:

from myproj.utils import *
getContext().set(...)

I got this error.

What should I do to correct that? See above :-) and post a follow-up to report if the issue is solved.

PS sorry for bad english. I am not a native speaker.
Jul 18 '05 #2

P: n/a
I have tried using "sysctx=None" instead of "global sysctx", but it
doesn't work either.
It seems my initialization work in the previous calling of init() has
no persistent effect when "utils" is imported using "from myproj.utils
import getContext".

What's weird, when a module is in the same directory as utils.py, where
I can simply use "utils" for importing, there is no such problem.

Any other suggestions?

Jul 18 '05 #3

P: n/a
Tian a écrit :
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
--------------------
global sysctx

class Context:
def __init__(self):
def set(self, name, obj, overwrite=True):
def get(self, name):
def has(self, name):

def init():
global sysctx
sysctx = Context()

def getContext():
global sysctx
return sysctx


Why using a (not so) global variable here ? If your problem is to make
sure you have only one instance of Context in your program, there are
cleaner solutions - one of them being a Singleton (you'll find all
needed doc and exemples on the net - google is your friend !-).
Jul 18 '05 #4

P: n/a
I googled about how to write singleton in python, but even if I use
Singleton, in which module's namespace should I keep the instance of
this singleton? suppose I have a singleton class definiton in
"utils.py", how should I import and where should I make instance and
initialize? Thanks!!

Jul 18 '05 #5

P: n/a
'
noob warning:
what is so wonderful about the NEW class over the old ?

Jul 18 '05 #6

P: n/a

"Tian" <wa*********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type


I suspect that you would accomplish your goal much more easily by calling
your module 'context' or something like that, importing it into every other
module that needs it, and then accessing values as attributes of context.
Don't reinvent the module ;-)

other.py

import context

context.a = 3

x = context.b # where context.b is previous set somewhere else

If you need multiple shared contexts, people subclass object

class context(object): pass

context1 = context()
context2 = context()

# now set and get attributes as needed

Terry J. Reedy

Jul 18 '05 #7

P: n/a
Tian wrote:
I have tried using "sysctx=None" instead of "global sysctx", but it
doesn't work either.
It seems my initialization work in the previous calling of init() has
no persistent effect when "utils" is imported using "from
myproj.utils import getContext".

What's weird, when a module is in the same directory as utils.py,
where I can simply use "utils" for importing, there is no such
problem.

Any other suggestions?


put the following print statement next to every "global sysctx"
replacing ... with the function name where the statement is located.

print "... globals are in %s" % __name__

Serge.

Jul 18 '05 #8

P: n/a
Tian a écrit :
I googled about how to write singleton in python, but even if I use
Singleton, in which module's namespace should I keep the instance of
this singleton?


You found the doc but I'm afraid you did not grasp the concept.

You don't have to 'keep the instance' anywhere - it's the job of the
singleton to do this. The principle of the Singleton is that you can
have *only one* instance of it.
Jul 18 '05 #9

P: n/a
'@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) a écrit :
noob warning:
what is so wonderful about the NEW class over the old ?


A whole lot of things. But the main thing to know is that old-style
classes are deprecated, and will disappear in the future.
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.