473,385 Members | 1,630 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,385 software developers and data experts.

globally available objects

Hi list,
I speak with ignorance but I'd like to know if something like
this is even possible:
I would like to create an object while importing a module only the
first time it get's imported. I wonder imports already work in this
manner. For example, in a module (say global_objs.py) I would have:

import mylogger
import error_hdlr

my_log_obj = mylogger.init_log() # This may involve some file I/O
my_err_obj = myerror_hdlr.init() # Thiis also might involve some file I/O

now, at run time, would mylogger.init_log() be called each for each
import of global_objs.py ?? I feel it would. Assuming this is correct,
how do I ensure that once created, the objects in global_objs.py do
not get recreated. ....maybe I need to redefine __import__ or
__getattr__ ?? If yes, how do I know from within such functions
whether the objects have been created ??

ehe ...am I making sense to anyone else here ??

Regards
Steve
Jul 18 '05 #1
3 1092
Steve wrote:

ehe ...am I making sense to anyone else here ??


You do, but if you put the same amount of time writing this message in
writing a simple test module for yourself and importing it, you've had seen
that it in fact works the way you want it: a module is only imported once.

No offense intended, btw - I just wanted to point out that in python its
common to simply fire up the interpreter and poke around to test things.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Hi Diez,

On Fri, 29 Oct 2004 12:58:19 +0200, Diez B. Roggisch <de*********@web.de> wrote:
Steve wrote:

ehe ...am I making sense to anyone else here ??
You do, but if you put the same amount of time writing this message in
writing a simple test module for yourself and importing it, you've had seen
that it in fact works the way you want it: a module is only imported once.

Yes I did know that, however, I was not sure if objects that are
created during imports would be the same. Anyways, I did create a test
scriptlets and verified that it indeed is that way !!!!
------------------------------------------------------------------------------------
[steve@sfernandez20 ~]$ cat foo.py
print "calling file().read()"
s = file('test').read()

[steve@sfernandez20 ~]$ cat first.py
#!/usr/bin/python
from foo import s
print id(s)

[steve@sfernandez20 ~]$ cat second.py
#!/usr/bin/python
from foo import s
print id(s)

[steve@sfernandez20 ~]$ python

Python 2.3.3 (#2, Feb 17 2004, 11:45:40)
[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import first, second calling file().read()
1077624320
1077624320

------------------------------------------------------------------------------------
No offense intended, btw - I just wanted to point out that in python its
common to simply fire up the interpreter and poke around to test things.


No offense taken, thanks for the suggestion. I did try some tests but
with the wrong intent. I guess I just got bogged down with the actual
way to emulate the behaviour (tried overloading __import__ and stuff
like that). Guess I need more coffee !!

In any case, I still would like to have a look at Pierre singleton
implementation,

Regards
Steve
Jul 18 '05 #3
> In any case, I still would like to have a look at Pierre singleton
implementation,


There are some recipes on aspn's cookbook - go google :)
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

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

Similar topics

7
by: Thomas Gagne | last post by:
I've been reading a lot about Seaside <http://www.seaside.st/> lately and before that Struts <http://struts.apache.org/>. Is there a predominant framework for PHP that provides many (most,...
21
by: steve | last post by:
Dont’ make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the...
13
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out...
1
by: Tony Archer | last post by:
I am trying to build a site for a group which already has a hosting company. I would like to try to find out which objects are available for use to me. I do not have admin access to the NT box. ...
1
by: Mike Kline | last post by:
Hi There! How do I make the Objects in the custom made HttpModule available to ASPX page or ASCX controls without requiring an object reference? For example, SessionState HttpModule made the...
1
by: Matt Kruse | last post by:
Given the following in the <body> of an HTML page: <form name="test" action="action"> <input name="test1" value="abc"> <script type="text/javascript"> alert(document.forms.elements.value);...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
2
by: robin.bruce | last post by:
Hi guys, I just tried to give advice to a colleague who has a problem program in C, and I was interested to hear the thoughts of this forum on the subject. I've been an occasional programmer...
1
by: jagan1976 | last post by:
I want to disable close button for each report and it would apply to all users. Is there any script or method available which will disable close button for all reports at once without using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.