[Tutor] Re: [Edu-sig] Python resources CD available [getting help() as a builtin]

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 10 Jun 2001 10:37:50 -0700 (PDT)


On Sat, 9 Jun 2001, Patrick K. O'Brien wrote:

> import __builtin__
> from pydoc import help
> __builtin__.help = help
> del help
> del __builtin__
> 
> When I go into an IDE that honors PYTHONSTARTUP, such as IDLE with the
> -s command line switch, help is added to builtin and works completely
> fine (see below for sample output).

Ah, good!  I'm glad it's working now.


> I must admit, I'm not sure I understand the advantage of having help
> be a builtin versus having it be just 'from pydoc import help' except
> that the latter did nothing when I tried it as part of site.py. So I
> imagine there are namespace things going on that I don't fully
> understand.

It's a namespacing issue: if we do the "from pydoc import help", then
help()'s visible from the 'site' module, since that's where it's being
imported into.  However, it's not visible anywhere outside, and that's the
big problem.  We can do a small example to explore this if you want.  
It's an extension of the "local variables" idea, but instead of functions,
it uses whole files (modules) as the containers.


The way the code gets around this isolating behavior is it grabs the
__builtin__ module, which is actually just a big container for all the
built-in functions that Python makes available to us globally.

###
>>> import __builtin__
>>> dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError',
'Exception', 'FloatingPointError', 'IOError', 'ImportError',
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'RuntimeError',
'RuntimeWarning', 'StandardError', 'SyntaxError', 'SyntaxWarning',
'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError',
'UnicodeError', 'UserWarning', 'ValueError', 'Warning',
'ZeroDivisionError', '__debug__', '__doc__', '__import__', '__name__',
'abs', 'apply', 'buffer', 'callable', 'chr', 'cmp', 'coerce', 'compile',
'complex', 'copyright', 'credits', 'delattr', 'dir', 'divmod', 'eval',
'execfile', 'exit', 'filter', 'float', 'getattr', 'globals', 'hasattr',
'hash', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass',
'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'oct',
'open', 'ord', 'pow', 'quit', 'range', 'raw_input', 'reduce', 'reload',
'repr', 'round', 'setattr', 'slice', 'str', 'tuple', 'type', 'unichr',
'unicode', 'vars', 'xrange', 'zip']
###


The code snippet that we have:

> import __builtin__
> from pydoc import help
> __builtin__.help = help

is meant to stuff our help function() alongside those other functions.



[about the unsuccessful approach in sitecustomize.py]

> but not when loaded from sitecustomize.py. So then I tried the original
> suggestion to add it to site.py and I get the same behavior. Can anyone else
> confirm this? Daniel? I ran this on Win98SE with Python 2.1.

Hmmm!  I haven't been able to confirm this; I don't have Windows on this
laptop.  I'll see if I can check this on Win2k when I get home though.