FAQ
Hello,

I have a program which consists of several little modules, but I encounter a
problem with that. I have one module which returns a class instance, with
messages as members, either Dutch or English. Every module needs that instance,
but I don't want to pass it as an argument to every little function, the
modules should find out theirselves. But I _also_ don't want to make that
instance 5 times.

So I thougt: I need a sort of global between the modules. Is this possible?

I'm now pickling and unpickling it, but that _very_ slow, of course:
bash:gerrit at gerrit:~/project/python/discoverb>>> time ./discoverb.py </dev/null
Welke file zal ik de woorden uit halen? Traceback (innermost last):
File "./discoverb.py", line 168, in ?
run()
File "./discoverb.py", line 145, in run
file = raw_input(mes.whichfile + " ")
EOFError: EOF when reading a line

real 0m2.504s
user 0m2.060s
sys 0m0.190s

more than 2 seconds, it _has_ to be possible faster to find out a language!

regards,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program (in several languages) which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
Oh my god! They killed init! You bastards!

Search Discussions

  • Robin Becker at Jul 7, 1999 at 12:45 pm
    In article <19990707100532.A951 at optiplex.palga.uucp>, Gerrit Holl
    <gerrit.holl at pobox.com> writes
    Hello,

    I have a program which consists of several little modules, but I encounter a
    problem with that. I have one module which returns a class instance, with
    messages as members, either Dutch or English. Every module needs that instance,
    but I don't want to pass it as an argument to every little function, the
    modules should find out theirselves. But I _also_ don't want to make that
    instance 5 times.

    So I thougt: I need a sort of global between the modules. Is this possible? ...
    regards,
    Gerrit.
    I guess you need a singleton class. There was a c.l.p thread about this
    recently.

    I suggest something of the form

    _LangGlobal=None

    def getMessages():
    global _LangGlobal
    if _LangGlobal is None:
    # calculate which language
    #...
    #...
    _LangGlobal=result
    return _LangGlobal

    --
    Robin Becker
  • Hans Nowak at Jul 7, 1999 at 12:59 pm

    On 7 Jul 99, Gerrit Holl wrote:

    Hello,

    I have a program which consists of several little modules, but I encounter a
    problem with that. I have one module which returns a class instance, with
    messages as members, either Dutch or English. Every module needs that instance,
    but I don't want to pass it as an argument to every little function, the
    modules should find out theirselves. But I _also_ don't want to make that
    instance 5 times.

    So I thougt: I need a sort of global between the modules. Is this possible?
    Why not use it like this:

    [module mymodule.py]
    class MyClass:
    # blah blah
    myinstance = MyClass()

    Now we have the module with your much-used instance. This can be used
    by other modules as follows:

    [module a.py]
    import mymodule

    mymodule.myinstance.do_something(x,y)

    But maybe you tried this approach already?

    Hans Nowak (ivnowa at hvision.nl)
    Homepage: http://fly.to/zephyrfalcon
  • Brad Howes at Jul 9, 1999 at 12:25 am

    Gerrit Holl <gerrit.holl at pobox.com> writes:

    I have a program which consists of several little modules, but I
    encounter a problem with that. I have one module which returns a
    class instance, with messages as members, either Dutch or
    English. Every module needs that instance, but I don't want to pass
    it as an argument to every little function, the modules should find
    out theirselves. But I _also_ don't want to make that instance 5
    times.

    So I thougt: I need a sort of global between the modules. Is this possible?
    This is what I've done before. Plop this at the bottom of the file
    that define your class (`Singleton' below) and just import this module
    everywhere else.

    import __main__
    if not hasattr( __main__.__builtins__, 'gMe' ):
    __main__.__builtins__.gMe = Singleton()

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 7, '99 at 8:05a
activeJul 9, '99 at 12:25a
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase