470,638 Members | 1,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Binding the names in a module in a class instance

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
1 1129
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.

Similar topics

6 posts views Thread by Mayer | last post: by
5 posts views Thread by ThunderMusic | last post: by
5 posts views Thread by Larry Bird | last post: by
6 posts views Thread by Tim Roberts | last post: by
4 posts views Thread by Rippo | last post: by
7 posts views Thread by bukzor | last post: by
26 posts views Thread by Aaron \Castironpi\ Brady | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.