473,850 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about import and namespace

Hi,

I'm new to Python and have the following problem :
I have an application started by a main.py file, which does a ' from
module_1 import * '.
main.py is responsible from the creation of an object which is then
used in module_1.
What is the best way to make that object visible in the module_1
namespace ?
I guess that if I do an ' import module_1 ', I can make the object
visible with ' module_1.myObje ct = myObject ', but that won't work with
the from ... import * statement.

Thanks in advance for your help.

Sep 1 '06 #1
6 1524
In <11************ *********@m79g2 000cwm.googlegr oups.com>, jdemoor wrote:
I have an application started by a main.py file, which does a ' from
module_1 import * '.
main.py is responsible from the creation of an object which is then
used in module_1.
What is the best way to make that object visible in the module_1
namespace ?
I guess that if I do an ' import module_1 ', I can make the object
visible with ' module_1.myObje ct = myObject ', but that won't work with
the from ... import * statement.
Give the object as argument to functions in `module_1` is a clean solution.

Ciao,
Marc 'BlackJack' Rintsch
Sep 1 '06 #2
jd*****@gmail.c om wrote:
I'm new to Python and have the following problem :
I have an application started by a main.py file, which does a ' from
module_1 import * '.
main.py is responsible from the creation of an object which is then
used in module_1.
What is the best way to make that object visible in the module_1
namespace ?
I guess that if I do an ' import module_1 ', I can make the object
visible with ' module_1.myObje ct = myObject ', but that won't work with
the from ... import * statement.
You can do both

from module import *
import module

as these kinds of import are not mutually exclusive.
But I recommend that you restructure your modules to avoid cyclic
dependencies.

Peter
Sep 1 '06 #3
Thanks for the replies.
You can do both

from module import *
import module

as these kinds of import are not mutually exclusive.
Would this run the code in 'module' twice, or just make the objects in
it accessible by several names ?

Sep 1 '06 #4
jd*****@gmail.c om wrote:
Thanks for the replies.
>You can do both

from module import *
import module

as these kinds of import are not mutually exclusive.

Would this run the code in 'module' twice, or just make the objects in
it accessible by several names ?
The latter. But why don't you try it yourself by putting a

print "importing module" # you will see that once

statement in the module?

There is one notable exception, the application's main module:

$ cat main.py
import main
print "importing main as", __name__
$ python main.py
importing main as main
importing main as __main__

Peter
Sep 1 '06 #5
In <11************ *********@i42g2 000cwa.googlegr oups.com>, jdemoor wrote:
>from module import *
import module

as these kinds of import are not mutually exclusive.

Would this run the code in 'module' twice, or just make the objects in
it accessible by several names ?
The code at module level is only executed at first import.

Ciao,
Marc 'BlackJack' Rintsch
Sep 1 '06 #6
Ok, thanks again. That was helpful.

Sep 1 '06 #7

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

Similar topics

2
1345
by: Aubrey Hutchison | last post by:
Question about functions: 1)--- module "A" contains the following X = 999666 #easy to notice value Y = 111222 #easy to notice value Product = 69696969 #easy to notice value def DoSomething(): global Product,X,Y Product = X*Y
2
1962
by: Fritz Bosch | last post by:
Hi experts Is is possible to import/manipulate a module such that I can supply its __dict__? I want to supply my own dict subclass object to be filled by the import, e.g. a class like: >>> class MyModuleDict(dict): .... def __setitem__(self,name,val):
5
2170
by: Pekka Niiranen | last post by:
Hi there, I have two scripts. The first "main.py" sets some variables and then imports another called "gen.py". The idea is to provide "main.py" that defines some paths, variables etc. without using Windows environment variables. Various other "hackers" will make additional Python scripts (subroutines) like "gen.py" that utilize variables set by the "main.py" and which "main.py" calls. I can do this with "subprocess" module by setting...
5
1181
by: KJ | last post by:
In an Xml schema: can an element have children which come from another namespace? For example, can this be declared in the schema: <top> <anotherNamespace:a> <aThirdNamespace:b> Hello World </aThirdNamespace:b>
1
2332
by: Fraggle | last post by:
I have a repeater with controls added at run time. the <template> also contains a <asp:textbox that is made visible on some repeater elements. when I come to read the text info out it has disapeared. The read is done on a button click. I can read the selected items from the other controls in the repeater, demo page here
3
1113
by: Katie S | last post by:
I have a method that takes a string, formats it and returns it I want to use this in multiple ASP.NET pages, by putting it in a namespace and using that in each asp.net page What I don't understand is how to write that namespace so that I can reference that method elsewhere by simply writing string Message = "Hello world"; string newstring = MyNamespace.MyMethod(Message) so I can call the method without having to create a new objec ...
7
1157
by: SL | last post by:
Could someone direct to a simple explanation of how to use MS Visual Studio 2003 to design .aspx code? This code works if pasted into notepad and run as an .aspx file: <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="vb" runat="server"> Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Dim objConnection As OleDBConnection Dim objCommand As OleDBDataAdapter
2
22558
by: Carmit | last post by:
Hi, I'm trying to build a proxy for this webservice: http://webservices.sabre.com/wsdl/sabreXML1.0.00/tpf/EndTransactionLLSRQ.wsdl I'm getting the following error: Error: Unable to import binding 'EndTransactionSoapBinding' from namespace 'http s://webservices.sabre.com/websvc'.
11
1512
by: Brian Blazer | last post by:
OK, I have a very simple class here: class Student: """Defines the student class""" def __init__(self, lName, fName, mi): self.lName = lName self.fName = fName self.mi = mi
0
1492
by: Eric_Dexter | last post by:
I know this might be the wrong place to ask but I recently modified the ogl example from wxpython and it works fine in spe but when I call it from other programs it wierds out because of the run.py thing they have set it up with doesn't work when I call it from an external program (including .bat files). when I call it outside of spe I manage to just get a box without the tracker part. It is so close to out right theft I was hopeing I...
0
9741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11011
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9503
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7071
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5735
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5929
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4546
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
2
4140
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3178
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.