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

Binding the names in a module in a class instance

P: n/a
Hello all,

I would like to be able to take a module full of class instances,
functions, etc and bind all its names to a separate container class in
a different module. I have come up with the following way to do it..

(module "globals")

class Container:
pass
container = Container()

(module "all_the_stuff")

....define a bunch of stuff...

(module "main")

exec open("all_the_stuff.py").read() in globals.container.__dict__

I feel uneasy about this method. I foresee bad namespace clashes.
What's a better way? :)

Thanks in advance,
Jacob

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Jacob H wrote:
Hello all,

I would like to be able to take a module full of class instances,
functions, etc and bind all its names to a separate container class in a different module. I have come up with the following way to do it..
[snip]
I feel uneasy about this method. I foresee bad namespace clashes.
What's a better way? :)


Perhaps this is more like what you are looking for:

import stuff # stuff we want to copy
import everything # initially empty module where to store stuff

# loop over each attribute name in stuff
for attr in dir(stuff):
# skip over __special__ attributes, probably don't want them
if attr.startswith('__') and attr.endswith('__'):
continue

value = getattr(stuff, attr)
setattr(everything, attr, value)

You can add more checking for clashes or whatever by looking at the
attribute name (attr) which is a string.

hth,
n

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.