Lazy Python usage (hack request)

Pearu Peterson pearu at
Sat Dec 30 08:04:23 EST 2000


Is it possible to achive the following behavior in Python?

>>> import lazy
>>> a = b
No name 'b' defined, using default 777
>>> print a

Note that changing the python session above is not an option. (Also 'No'
for the answer;-)

Just to avoid questions 'Why do you need it?', I would use it in
PySymbolic (but not in its Kernel!) for defining new Symbol instances in
fly (it's very tedious to define 'b = Symbol("b")',etc all the time).

Here would be something like the following:
#Begin of

import UserDict,sys

class myDict(UserDict.UserDict):
    def __getitem__(self, key):
        if not
            print 'No name %s defined, using default 777'%(`key`)
  [key] = 777

sys.modules['__builtin__'].__dict__ = myDict(sys.modules['__builtin__'].__dict__)


When trying to set sys.modules['__builtin__'].__dict__, an exception
	TypeError: read-only special attribute
is raised.

Is there anyway to go around that? May be in C level?

Any hints, suggestions, or similar are appreciated.


More information about the Python-list mailing list