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

Patrick K. O'Brien pobrien@orbtech.com
Sun, 10 Jun 2001 13:22:07 -0500


Just to clarify a tiny bit. (I hope) Adding help to the builtins works from
my PYTHONSTARTUP file, but not from site.py or sitecustomize.py. So it
doesn't really have any advantage over 'from pydoc import help' when loaded
this way, does it? If it will only work properly from the startup file, I
feel that "from pydoc import help" is just as good an approach. In fact, it
has the advantage of being listed in dir() and not buried in __builtins__.
Or am I missing something?

I think I understand the namespace concept pretty well. (I hope. <g>) What I
don't understand is why the module pointed to by PYTHONSTARTUP is part of
the global namespace (which is why we can just do 'from pydoc import help'
in it, and the help function stays around) while site.py is not (so while
'from pydoc import help' works it also "disappears" once control goes to the
interactive prompt, unless we do the __builtin__ trick). Well, I do
understand why it works that way for the startup file. It just seemed like
it would work the same for site.py, based on the (skimpy) documentation. So
my confusion has more to do with a lack of documented behavior, rather than
the behavior itself, if that makes any sense. And maybe the answer is just
"because".

Other than that, I understand and agree with everything you say below. I
just wish I knew why your suggested code doesn't entirely work in site.py.

---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Daniel Yoo
Sent: Sunday, June 10, 2001 12:38 PM
To: Patrick K. O'Brien
Cc: tutor@python.org
Subject: RE: [Tutor] Re: [Edu-sig] Python resources CD available [getting
help() as a builtin]

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.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor