Having a __stdlib__ namespace?

Bengt Richter bokr at oz.net
Thu Jan 9 23:35:36 EST 2003


On Fri, 10 Jan 2003 00:15:46 +0000 (UTC), Daniel Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:

>
>I often find that the modules in my own directory can be misnamed to
>conflict with those in the Standard Library.  For example, I might
>have a file called 'random.py' in my working directory, which obscures
>the Standard Library module.
>
>
>Would it be a good idea to have a '__stdlib__' module that magically
>maps its attributes to only Standard Library modules?  It would act as
>a namespace to guarantee access to the standard library, no matter how
>weird the sys.path might get.
>
>
>For example:
>
>###
>import __stdlib__.random      ## guarantees that we're using the
>                              ## standard library module to look this up
>###
>
>
>I wanted to get some feedback on this before writing a PEP... *grin*

I just did an experiment that I don't know all the ramifications of, so don't
take this is anything but an expriment, but if you put a file called
__init__.py with nothing in it (mine has two bytes: '\r\n') in the Lib
directory, so the path to it looks something like 

    D:\Python22\Lib\__init__.py

(your installation location may vary)

Then you can **apparently** import Lib.random in much the way I think you
are asking for.

 >>> import Lib.random
 >>> dir(Lib)
 ['__builtins__', '__doc__', '__file__', '__name__', '__path__', 'random']
 >>> Lib.random.choice('which word is it going to be?'.split())
 'be?'
 >>> Lib.random.choice('which word is it going to be?'.split())
 'word'
 >>> Lib.random.choice('which word is it going to be?'.split())


 >>> from Lib import random as r
 >>> import random as r2
 >>> r
 <module 'Lib.random' from 'D:\python22\Lib\random.pyc'>
 >>> r2
 <module 'random' from 'D:\python22\lib\random.pyc'>
 >>> Lib.random
 <module 'Lib.random' from 'D:\python22\Lib\random.pyc'>

I'm not sure what pitfalls there are, but maybe someone else has tried it, or
is using something like it. But it does make two ways of getting to the same thing,
depending on how your sys.path is set up.

Regards,
Bengt Richter




More information about the Python-list mailing list