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.Name Error'>: 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 1350
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.Name Error'>: 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(lo cals())
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.Name Error'>: 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)(*a rgs,
**kw)
return g
@snatch_globals
def doit():
print 2 * a
Peter
On Sun, 25 May 2008 03:32:30 -0700 (PDT), ul****@dorda.ne t 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.ne t 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 just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?
I've...
|
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 appreciated.
Thanks in advance
|
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: PublicSite.MyProfile.Business (This contains the business
rules)
Root namespace: PublicSite
|
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
declared in a class outside of any subs or functions... below " Windows Form
Designer generated code " with the Public keyword and I could use this...
| |
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 Class2.vb)
abc.xyz.Class3 (in Class3.vb)
....
abc.xyz.Class99 (in Class99.vb)
|
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 called "shrink_bs_070226" that looks like this:
**********************************
import sys, re, glob, shutil
import os
|
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"
that can be called during the interactive session using locals() as an
argument, as follows:
restart(locals())
It works. However, I would...
|
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 the cursor inside the mark to enter the string,in
windows,it is ok.but when i do that in linux,pressing the "left" key
will just print "^[[D" in the...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |