I've got a probably embarrassing trivial problem with namespaces, but couldn't solve it
myself nor find an answer in the net. Hopefully one of you guys can help me.
What I want to do:
Use the interactive shell and e.g define the variable a there.
Then load a module and access a from within.
e.g file "utest.py"
def doit():
print 2*a
in the shell:
import utest
a=3
utest.doit() <- I want this to print 2*a, but of course obtain: <type
exceptions.NameError'>: global name 'a' is not defined
Any change I do to a in the shell should be seen from the doit() function, any variable
assignment I do in the doit() function should be seen in the shell. I guess it's somehow a
namespace sharing.
Actually the function doit() will contain an eval() function that should evaluate a (via a
gui) dynamically inserted expression.
Any one got a clue? (a clue what I try to say and how to help?!)
Thanks a lot in advance!!
Ulrich 6 1338
Ulrich Dorda wrote:
I've got a probably embarrassing trivial problem with namespaces, but
couldn't solve it myself nor find an answer in the net. Hopefully one of
you guys can help me.
What I want to do:
Use the interactive shell and e.g define the variable a there.
Then load a module and access a from within.
e.g file "utest.py"
def doit():
print 2*a
in the shell:
import utest
a=3
utest.doit() <- I want this to print 2*a, but of course obtain: <type
exceptions.NameError'>: global name 'a' is not defined
Any change I do to a in the shell should be seen from the doit()
function, any variable assignment I do in the doit() function should be
seen in the shell. I guess it's somehow a namespace sharing.
Actually the function doit() will contain an eval() function that should
evaluate a (via a gui) dynamically inserted expression.
Any one got a clue? (a clue what I try to say and how to help?!)
Thanks a lot in advance!!
Ulrich
Here is one way
#utest.py:
def doit(valuemap):
print 2*valuemap['a']
Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a = 4 import utest utest.doit(locals())
8
>>>
Thanks for the reply,
Of course the suggested solution is working and good, but a bit
complicated. The module/function where i need to access the variable
value from the interactive shell is burried quite deep and I would
nedd to hand the locals() quite often from one module to another.
Furthermore it makes the call function slightly more complicated, as
the locals()-argunment has to be given every time.
I was hoping for something a bit different: If I wanted to access a
value b from another module "utest2.py", I would simply need to type
in utest.py: import utest2; print 2*utest2.b
Isn't there a name for the interactive namespace (like here the
utest2), which I can use to access the variable without handing the
whole dictionary?
Cheers,
Ulrich
Ulrich Dorda wrote:
I've got a probably embarrassing trivial problem with namespaces, but
couldn't solve it myself nor find an answer in the net. Hopefully one of
you guys can help me.
What I want to do:
Use the interactive shell and e.g define the variable a there.
Then load a module and access a from within.
e.g file "utest.py"
def doit():
print 2*a
in the shell:
import utest
a=3
utest.doit() <- I want this to print 2*a, but of course obtain: <type
exceptions.NameError'>: global name 'a' is not defined
Any change I do to a in the shell should be seen from the doit() function,
any variable assignment I do in the doit() function should be seen in the
shell. I guess it's somehow a namespace sharing.
Actually the function doit() will contain an eval() function that should
evaluate a (via a gui) dynamically inserted expression.
Any one got a clue? (a clue what I try to say and how to help?!)
Thanks a lot in advance!!
While the sane approach to this is
def doit(a):
print 2 * a
here is an insane one:
import sys
def f(): pass
function = type(f)
def snatch_globals(f):
def g(*args, **kw):
return function(f.func_code, sys._getframe(1).f_globals)(*args,
**kw)
return g
@snatch_globals
def doit():
print 2 * a
Peter
On Sun, 25 May 2008 03:32:30 -0700 (PDT), ul****@dorda.net wrote:
>Thanks for the reply,
Of course the suggested solution is working and good, but a bit complicated. The module/function where i need to access the variable value from the interactive shell is burried quite deep and I would nedd to hand the locals() quite often from one module to another. Furthermore it makes the call function slightly more complicated, as the locals()-argunment has to be given every time.
I was hoping for something a bit different: If I wanted to access a value b from another module "utest2.py", I would simply need to type in utest.py: import utest2; print 2*utest2.b Isn't there a name for the interactive namespace (like here the utest2), which I can use to access the variable without handing the whole dictionary?
"""utest.py"""
import __main__
def doit():
print 2*__main__.a
>Cheers,
Ulrich
David C. Ullrich
Thanks a lot to all!
Apart from obtaining the solution I was searching for, I learned a lot
by studying your answers!
Cheers,
Ulrich
On Sun, 25 May 2008 05:56:37 -0700 (PDT), ul****@dorda.net wrote:
>Thanks a lot to all!
Apart from obtaining the solution I was searching for, I learned a lot by studying your answers!
Since my solution seems to have been the one you were looking
for (I was surprised nobody else suggested it) maybe I should
enphasize that people better informed than me had the same
reaction as I did to your question:
While this is something you can do if you want it's reallly hard
to see why it's a good idea - simply passing data to functions
in the other unit as parameters seems better.
>Cheers,
Ulrich
David C. Ullrich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe |
last post by:
When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.
Is there a way to have this occur from a running program?
In other words can I...
|
by: Dan Elliott |
last post by:
Hello,
Converting from a working C program to C++, I run into the following error:
I have a header: (header.h)
namespace shared{
...
struct X{
...
|
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
by: SteveS |
last post by:
Hello All. I have an asp.net application with 3 different assemblies. They
are like this:
1) Assembly: PublicSite (This contains the website UI)
Root namespace: PublicSite
2) Assembly:...
|
by: dotNETnews |
last post by:
Little confused...
When I declare a namespace variable in module I can use it anywhere in the
project. That's clear.
I read a book in which the author said that namespace variables can be...
|
by: Mark Denardo |
last post by:
Ok here's my problem:
I have a bunch of Classes at the same namespace level say "abc.xyz". And
all Classes reside in different files.
abc.xyz.Class1 (in Class1.vb)
abc.xyz.Class2 (in...
|
by: boris.smirnov |
last post by:
Hi there,
I have a problem with setting environment variable in my script that
uses qt library. For this library I have to define a path to tell the
script whre to find it.
I have a script...
|
by: =?iso-8859-1?B?QW5kcuk=?= |
last post by:
I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"...
|
by: yan.python |
last post by:
i have a question.
when i run Interactive Interpreter in linux command promt,how can i
move the cursor.
for example,when i enter a string,i often enter the quotation mark ""
first,and the move...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |