Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > namespace question

Reply
Thread Tools

namespace question

 
 
Elaine Jackson
Guest
Posts: n/a
 
      09-02-2004
I would like to be able to write a function f, which will live in a module M,
and will call a function g, such that, when f is imported from M into the
interpreter, and invoked there, its invokation of g will return the
interpreter's global namespace. Is there a way to do this and, if so, how?
Muchas gracias for any and all assistance.

Peace


 
Reply With Quote
 
 
 
 
Phil Frost
Guest
Posts: n/a
 
      09-02-2004
I'm not sure I understand what you are asking, but maybe globals() is
what you want?

On Thu, Sep 02, 2004 at 01:52:46PM +0000, Elaine Jackson wrote:
> I would like to be able to write a function f, which will live in a module M,
> and will call a function g, such that, when f is imported from M into the
> interpreter, and invoked there, its invokation of g will return the
> interpreter's global namespace. Is there a way to do this and, if so, how?
> Muchas gracias for any and all assistance.
>
> Peace

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      09-02-2004
Elaine Jackson wrote:
> I would like to be able to write a function f, which will live in a module M,
> and will call a function g, such that, when f is imported from M into the
> interpreter, and invoked there, its invokation of g will return the
> interpreter's global namespace.


What does that last part mean? There is no such thing as the
"interpreter's global namespace". Each module is its own namespace.

-Peter
 
Reply With Quote
 
Elaine Jackson
Guest
Posts: n/a
 
      09-03-2004
By "the interpreter's global namespace" I mean the dictionary that gets returned
if you evaluate 'globals()' in the interpreter.

"Peter Hansen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Elaine Jackson wrote:
| > I would like to be able to write a function f, which will live in a module
M,
| > and will call a function g, such that, when f is imported from M into the
| > interpreter, and invoked there, its invokation of g will return the
| > interpreter's global namespace.
|
| What does that last part mean? There is no such thing as the
| "interpreter's global namespace". Each module is its own namespace.
|
| -Peter


 
Reply With Quote
 
Elaine Jackson
Guest
Posts: n/a
 
      09-03-2004
It's what I want in the sense that it returns the correct dictionary when
globals is invoked interactively. What I want is to have a function that can be
imported into the interpreter and that will return that same dictionary.

"Phil Frost" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| I'm not sure I understand what you are asking, but maybe globals() is
| what you want?
|
| On Thu, Sep 02, 2004 at 01:52:46PM +0000, Elaine Jackson wrote:
| > I would like to be able to write a function f, which will live in a module
M,
| > and will call a function g, such that, when f is imported from M into the
| > interpreter, and invoked there, its invokation of g will return the
| > interpreter's global namespace. Is there a way to do this and, if so, how?
| > Muchas gracias for any and all assistance.
| >
| > Peace


 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      09-03-2004
Elaine Jackson <(E-Mail Removed)> wrote:

> I would like to be able to write a function f, which will live in a module M,
> and will call a function g, such that, when f is imported from M into the
> interpreter, and invoked there, its invokation of g will return the
> interpreter's global namespace. Is there a way to do this and, if so, how?
> Muchas gracias for any and all assistance.


There is no "interpreter global namespace". If you mean the namespace
of the calling module (which could be the interactive session's __main__
if that's who did the importing) you can get there via sys._getframe --
it's meant for debugging purposes (note the leading underscore) but it
will work. If you mean the namespace of __main__ (which could be the
interactive session if there is one) you can get the __main__ module
anywhere as sys.modules['__main__'] and a vars call around that will
give the module's dictionary (which is, I assume, what you mean by
namespace).


Alex
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      09-03-2004
Elaine Jackson wrote:

> By "the interpreter's global namespace" I mean the dictionary that gets returned
> if you evaluate 'globals()' in the interpreter.


As Alex said then:

(in module nstest.py):
import __main__
vars(__main__)['spam'] = 'baz'

(in interactive session with extra spacing for readability):
c:\>python
>>> globals()

{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', '__doc__': None}

>>> import nstest


>>> dir()

['__builtins__', '__doc__', '__name__', 'nstest', 'spam']

>>> globals()

{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'nstest': <module 'nstest' from 'nstest.py'>, '__doc__':
None, 'spam': 'baz'}



-Peter
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      09-04-2004
Elaine Jackson <(E-Mail Removed)> wrote:

> It's what I want in the sense that it returns the correct dictionary when
> globals is invoked interactively. What I want is to have a function that
> can be
> imported into the interpreter and that will return that same dictionary.


so what's wrong with:

def main_dictionary():
import __main__
return vars(__main__)


???
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      09-04-2004
On Fri, 3 Sep 2004 08:11:35 +0200, http://www.velocityreviews.com/forums/(E-Mail Removed) (Alex Martelli) wrote:

>Elaine Jackson <(E-Mail Removed)> wrote:
>
>> I would like to be able to write a function f, which will live in a module M,
>> and will call a function g, such that, when f is imported from M into the
>> interpreter, and invoked there, its invokation of g will return the
>> interpreter's global namespace. Is there a way to do this and, if so, how?
>> Muchas gracias for any and all assistance.

>
>There is no "interpreter global namespace". If you mean the namespace
>of the calling module (which could be the interactive session's __main__
>if that's who did the importing) you can get there via sys._getframe --
>it's meant for debugging purposes (note the leading underscore) but it
>will work. If you mean the namespace of __main__ (which could be the
>interactive session if there is one) you can get the __main__ module
>anywhere as sys.modules['__main__'] and a vars call around that will
>give the module's dictionary (which is, I assume, what you mean by
>namespace).
>

This brings up the question of standard terminology (e.g. for namespace).
If one googles for python glossary, the top references are mostly

http://manatee.mojam.com/python-glossary

which seems not to have caught on so far (mea culpa too, since it takes volunteers ;-/ ).

Anyway, my take on 'namespace' is a name->value mapping. I.e., pretty abstract.
Then follows a discussion of various python mechanisms that do that (whether via
a single dict's d[name] mapping or the infinite d.get(name, default) mapping,
or the mapping implicit in the search, e.g., for a bare name using a function's
local name lookup rules (local, closure, global, builtin) -- not to mention the
mapping names undergo when used as attribute names in various ways.

Compared to other languages, python's name spaces (in my sense) seem to have
evolved interestingly I am wondering whether the future holds a more unified
model of namespaces and their access mechanisms, so most inspect and sys._getframe
name access hacks will be obviated.

Re defintions, perhaps it is time to reinvigorate Skip's wiki or something like that?

There was an interesting thread re improving python doc functionality at

http://mail.python.org/pipermail/pyt...ay/219682.html

An online extension of help() maybe taking a syntax clue from google ,like

help('definexx')

might be an interesting hook into a glossary of definitions.

Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments