473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

namespace question

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
Jul 18 '05 #1
8 2049
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

Jul 18 '05 #2
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
Jul 18 '05 #3
By "the interpreter's global namespace" I mean the dictionary that gets returned
if you evaluate 'globals()' in the interpreter.

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:Ua********************@powergate.ca...
| 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
Jul 18 '05 #4
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" <in****@bitglue.com> wrote in message
news:ma**************************************@pyth on.org...
| 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
Jul 18 '05 #5
Elaine Jackson <el***************@home.com> 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
Jul 18 '05 #6
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
Jul 18 '05 #7
Elaine Jackson <el***************@home.com> 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__)
???
Jul 18 '05 #8
On Fri, 3 Sep 2004 08:11:35 +0200, al*****@yahoo.com (Alex Martelli) wrote:
Elaine Jackson <el***************@home.com> 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('define:xxx')

might be an interesting hook into a glossary of definitions.

Regards,
Bengt Richter
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
25
by: kj | last post by:
Consider the following XML document: <?xml version='1.0' encoding='UTF-8'?> <bar:foo xmlns:bar='someuri'> <baz/> </bar:foo> What namespace does baz belong to? What is this namespace bound...
3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
6
by: clinton__bill | last post by:
Hi, I usually use "using namespace <namespace_name>" to reference a namespace. Today I run across a code, in its header file it has this, namespace SP1{ class C1; } While SP1::C1 is a...
2
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
4
by: Programatix | last post by:
Hi, Normally, I would use Namespace for all the window forms I created. With VS 2005, the code generated by the designer is hidden using partial keyword. The question is, is there an efficient...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
3
by: CrazyJohn | last post by:
Hi guys, This is my first time posting question here, if I break any rules, please kindly point out. And I'm really glad to be a part of this community. Here is my question, Our lecturer...
17
by: Peng Yu | last post by:
Hi, I'm wondering if there is something in namespace like the 'private' keyword in class? I want to define some class or function that can only be used within that namespace. Thanks, Peng
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.