Modifying name lookup in console

ffdsfdsfds fdsdfs at dasdsa.com
Mon Nov 4 13:09:17 EST 2002


Hi, 
I try to get the following behavior in a python toplevel :
It should do
>>> x = 3
>>> print x
3
>>> print y
y
>>> print foo + bar
foobar

I tried the following code (which doesn't work), and I would
like to know if someone has any suggestions, not requiring to
patch python...

from code import *
from types import *

class MyDict(DictType):
    def __getitem__(self,x):
        print "getitem" + str(x)
        try:
            return DictType.__getitem__(self,x)
        except:
            return str(x)
    
    def get(self,x):
        print "get" + str(x)
        try:
            return DictType.__getitem__(self,x)
        except:
            return str(x)
    
    def __contains__(self,x):
        print "contains " + x
        return 1

console = InteractiveConsole(MyDict(locals()))
console.interact()

### In the new console
>>> print locals()['x']
x
>>> print x
Traceback (most recent call last):
  File "<console>", line 1, in ?
NameError: name 'x' is not defined


                                Sylvain B.



More information about the Python-list mailing list