[Python-3000] Change to class construction?
Phillip J. Eby
pje at telecommunity.com
Tue Jul 10 02:48:54 CEST 2007
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...
More information about the Python-3000
mailing list