473,545 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

access interactive namespace from module (shared namespace?)

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
Jun 27 '08 #1
6 1349
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
>>>
Jun 27 '08 #2
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

Jun 27 '08 #3
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
Jun 27 '08 #4
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
Jun 27 '08 #5
Thanks a lot to all!

Apart from obtaining the solution I was searching for, I learned a lot
by studying your answers!

Cheers,

Ulrich
Jun 27 '08 #6
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
Jun 27 '08 #7

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

Similar topics

20
2425
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...
4
7102
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{ ...
6
4718
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
6
2234
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
3
4028
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...
1
1152
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)
9
3737
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
2
2951
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...
4
3292
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...
0
7457
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7746
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5962
agi2029
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...
0
4941
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...
0
3443
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...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
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
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.