[Python-3000] Change to class construction? (original) (raw)

Ron Adam rrr at ronadam.com
Tue Jul 10 06:03:04 CEST 2007


Phillip J. Eby wrote:

At 07:40 PM 7/9/2007 -0500, Ron Adam wrote:

Guido van Rossum wrote:

We could easily change this to return a writable mapping that's not a dict at all but a "view" on the locals just as dict.keys() returns a view on a dict. I don't see why locals() couldn't return the object used to represent the namespace, but I don't see that it couldn't be some view on that object either, depending on the details of the implementation. This sounds great! I just recently wanted to pass a namespace to exec, but it refuses to accept anything but a dictionary for a local name space. You can already do that in Python 2.4. What I really want to do is pass an object as the local namespace. And have the exec() use it complete with it's properties intact. Passing obj.dict doesn't work in this case. You need a wrapper, e.g.: class AttrMap(object): def init(self, ob): self.ob = ob def getitem(self, key): try: return getattr(self.ob, key) except AttributeError: raise KeyError, key # setitem, delitem, etc...

Thanks, that should solves (I hope) the particular case I have. Although it would have been nicer if it was in the library someplace. Of course everyone says that about nearly everything.

It might be nice if locals() could receive an argument so it can be used with class's. Possible returning a wrapped class view such as the example you gave.

Regards, Ron



More information about the Python-3000 mailing list