what's so difficult about namespace?

Steve Holden steve at holdenweb.com
Wed Nov 26 10:50:14 EST 2008


Xah Lee wrote:
> On Nov 26, 5:45 am, Joshua Cranmer <Pidgeo... at verizon.invalid> wrote:
>>> i cannot fathom what could possibly be difficult of
>>> introducing or implementing a namespace mechanism into a language.
>> Namespaces go to the very core of a language, name resolution.
>> Retroactively adding such a feature is extremely difficult because there
>> is a strong chance of accidentally breaking existing code.
> 
> could you give some technical detail on this?
> 
> i don't know compiler so am probably being foolish here... but i
> suppose ultimately name resolution and storage at heart is something
> like a hashtable...  namely,
> 
> put ‹identifier 1› table
> put ‹identifier 2› table
> 
> and lookup is just
> 
> retrieve ‹identifier› table
> 
> and now suppose we introduced namespace, so i imagine the compiler
> simply just concat namespace component befor put, and split before
> retrieve?
> 
And therein lies the danger of imagination.

Typically the namespace components are looked up one by one, so for

   this.that.theother

first "this" will be looked up and (hopefully) yield an object with a
namespace, which will then be searched for "that", yielding another
object with a namespace in which "theother" can be looked up. That's
certainly how it works in Python:

>>> import os
>>> dir(os)
[... , 'path', ...]
>>> dir(os.path)
[... , 'walk']
<module 'os' from '/usr/lib/python2.4/os.pyc'>
>>> os.path
<module 'posixpath' from '/usr/lib/python2.4/posixpath.pyc'>
>>> os.path.walk
<function walk at 0xb7c4995c>
>>>

Using a single global namespace table might work, but only for some
horrendously convoluted values of "work".  To delete os.path you would
have to delete not only the key "os.path" but also all keys beginning
with "os,path." -- it's the kind of solution a PHP programmer might have
dreamed up.

And before anyone bothers to point it out, yes, I know PHO now (finally)
has namespaces.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list