Python Forum
what is the purpose of the global name space?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is the purpose of the global name space?
#11
(Oct-17-2018, 02:24 AM)Skaperen Wrote: there are three files.
So you need to change the title of this thread, because there is no global namespace in your sense. There is a global namespace at file scope, but three files define three global namespaces. You can exchange data between the functions by sharing a common object in these namespaces, for example
# haad.py
common = type('Common', (), {})()
common.a = 1
common.b = 2
and
# ien.py
from __main__ import common

def ien():
    common.c = 3
    common.d = 4
Reply
#12
i don't know how to change the title of this thread.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
I can change it for you. How do you want to formulate the subject?
Reply
#14
Python gives two main tools for dealing with namespaces: modules and classes. Gribouillis pointed out how you can rearrange your modules to give you the access you want. This is a major consideration in any large project: how am I going to arrange my modules so they can communicate in the way I need them to? My first thought is to use classes, which I feel give you more control:

main.py:

from foo import Ien
from bar import Twa


class Main(object):

    def __init__(self, modules = []):
        self.a = 1
        self.b = 2
        self.modules = [module(self) for module in modules]

    def act(self):
        for module in self.modules:
            module.act()


if __name__ == '__main__':
    main = Main([Ien, Twa])
    main.act()
foo.py:

class Ien(object):

	def __init__(self, master):
		self.master = master

	def act(self):
		self.master.c = 3
		self.master.d = 4
bar.py:

class Twa(object):

	def __init__(self, master):
		self.master = master

	def act(self):
		for attr in ('a', 'b', 'c', 'd'):
			try:
				print('{} = {}'.format(attr, getattr(self.master, attr)))
			except AttributeError:
				print('{} mislearre'.format(attr))
The output from running main.py:

Output:
a = 1 b = 2 c = 3 d = 4
In a real program, Ien and Twa would be subclasses of a Module object that would provide some basic functionality, like the shared __init__. If the modules need to communicate with each other, they would look at self.master.modules to find the module to communicate with, and get a reference to the object from there. In that case you might want a different structure to Main.modules that would be easier to search (like a dict). You could also set that as an attribute of the individual modules during Main.__init__, so that it is easier to access. But I would only do that if the set of modules is going to be static. Another way to do it is to have helper methods that find the module that another module needs.

Note that namespaces for modules and classes are implemented as dictionaries. So you could probably figure out a way to implement this using just nested dictionaries. But it's easier to just use the higher level constructs of modules and classes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
i need to do this in a way that can be done without any change to the calling file(s) (not mine) and without any disk I/O. the only idea i can up with real quick is to set/get environment variables. i coded the caller that does the testing, but in the real case this is code coming in from elsewhere as a bunch of .pyo files that has a simplistic config that lists what module.function to be called for specific hooks. it extracts the module name to know what to import (i presume). changing PYTHONPATH before this thing runs is an option but i doubt if that can help.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
(Oct-17-2018, 06:58 PM)Skaperen Wrote: i need to do this in a way that can be done without any change to the calling file(s) (not mine) and without any disk I/O.
What do you need to do exactly? Can you give a complete example?
Reply
#17
there is a program (in a .pyo file) that can be configured to call specific functions of specific modules at specific hooks (by name) in the program. a particular hook defines the conditions of being called and what it expects to be returned. for some hooks i have many modules to choose from. for other hooks not so many or maybe just one. i want to let the user copy or link the module they want at certain places. i need to have a common data area that is shared by all the modules. some set or change the values. most read most values. one of the values is a large event list that gets appended to by many of the modules, what is needed is a way for every function (or class) in every module to access the common name space. i was expecting the global name space to be it. i thought of using the environment variable space but it is rather limited. so what if i make a module that all the others import define a class in it. can i then use this as a common reference space and write code in the many modules like: common.foo = 42
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#18
(Oct-18-2018, 02:41 AM)Skaperen Wrote: so what if i make a module that all the others import define a class in it
Yes it is probably the best way to share data between all these modules.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020