globally available objects

Discussion in 'Python' started by Steve, Oct 29, 2004.

  1. Steve

    Steve Guest

    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
     
    Steve, Oct 29, 2004
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Oct 29, 2004
    #2
    1. Advertising

  3. Steve

    Steve Guest

    Hi Diez,

    On Fri, 29 Oct 2004 12:58:19 +0200, Diez B. Roggisch <> 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
     
    Steve, Oct 29, 2004
    #3
  4. > 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
     
    Diez B. Roggisch, Oct 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Harrad
    Replies:
    5
    Views:
    4,551
    Mike Treseler
    Dec 22, 2003
  2. valentin tihomirov

    Actual is not a globally static expression

    valentin tihomirov, Feb 10, 2004, in forum: VHDL
    Replies:
    7
    Views:
    8,894
    Jonathan Bromley
    Feb 17, 2004
  3. Jean-François Doyon

    Making classes from Metaclasses globally available

    Jean-François Doyon, Jun 16, 2004, in forum: Python
    Replies:
    13
    Views:
    497
    Michele Simionato
    Jun 17, 2004
  4. =?Utf-8?B?QmlnSm9obg==?=

    VB Modules Not Available Globally After Upgrade to 2005

    =?Utf-8?B?QmlnSm9obg==?=, Jul 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    318
    =?Utf-8?B?QmlnSm9obg==?=
    Jul 31, 2006
  5. Replies:
    1
    Views:
    123
    Dave Angel
    May 8, 2013
Loading...

Share This Page