an example of a singleton design pattern in python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Ortmann

    an example of a singleton design pattern in python?

    Hello,

    Does anyone have an example of a singleton design pattern in python?

    Thanks!

    --
    Daniel Ortmann, LSI Logic, 3425 40th Av NW, Suite 200, Rochester MN 55901
    work: Daniel.Ortmann@ lsil.com / 507.535.3861 / 63861 int / 8012.3861 gdds
    home: ortmann@venture cs.net / 507.288.7732, 2414 30Av NW #D, Rochester MN 55901
    gpg/pgp public key: http://wwwkeys.us.pgp.net
    jabber: daniel_ortmann@ jabber.org / dortmann@jabber .co.lsil.com
  • Rene Pijlman

    #2
    Re: an example of a singleton design pattern in python?

    Daniel Ortmann:[color=blue]
    >Does anyone have an example of a singleton design pattern in python?[/color]



    --
    René Pijlman

    Comment

    • Lawrence Oluyede

      #3
      Re: an example of a singleton design pattern in python?

      Daniel Ortmann <dortmann@lsil. com> writes:
      [color=blue]
      > Does anyone have an example of a singleton design pattern in python?[/color]

      Take a look at "Thinking in python"


      --
      Lawrence "Rhymes" Oluyede
      Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

      Comment

      • Yermat

        #4
        Re: an example of a singleton design pattern in python?

        Daniel Ortmann a écrit :
        [color=blue]
        > Hello,
        >
        > Does anyone have an example of a singleton design pattern in python?
        >
        > Thanks!
        >[/color]


        Look for links too on page :


        Hope it will help !
        Yermat

        Comment

        • Ville Vainio

          #5
          Re: an example of a singleton design pattern in python?

          >>>>> "Daniel" == Daniel Ortmann <dortmann@lsil. com> writes:

          Daniel> Hello, Does anyone have an example of a singleton design
          Daniel> pattern in python?


          This trivial snippet might be enough for your needs:

          class Foo():
          pass

          _inst = None

          def fooinstance():
          if not _inst:
          _inst = Foo()
          return _inst

          --
          Ville Vainio http://tinyurl.com/2prnb

          Comment

          • John Roth

            #6
            Re: an example of a singleton design pattern in python?


            "Rene Pijlman" <reply.in.the.n ewsgroup@my.add ress.is.invalid > wrote in
            message news:645r00hics lg2qn55sncentdk 8alnbevab@4ax.c om...[color=blue]
            > Daniel Ortmann:[color=green]
            > >Does anyone have an example of a singleton design pattern in python?[/color]
            >
            >[/color]


            The "cannonical " singleton for new style classes is in:



            See the section on overriding the __new__() method.

            John Roth[color=blue]
            >
            > --
            > René Pijlman[/color]


            Comment

            • Peter Hansen

              #7
              Re: an example of a singleton design pattern in python?

              Ville Vainio wrote:[color=blue]
              >[color=green][color=darkred]
              > >>>>> "Daniel" == Daniel Ortmann <dortmann@lsil. com> writes:[/color][/color]
              >
              > Daniel> Hello, Does anyone have an example of a singleton design
              > Daniel> pattern in python?
              >
              > This trivial snippet might be enough for your needs:
              >
              > class Foo():
              > pass
              >
              > _inst = None
              >
              > def fooinstance():
              > if not _inst:
              > _inst = Foo()
              > return _inst[/color]

              Looks like at least two errors, one being the parentheses on the
              class name, and the other being a failure to flag _inst as being
              global...

              -Peter

              Comment

              • Ville Vainio

                #8
                Re: an example of a singleton design pattern in python?

                [color=blue][color=green]
                >> This trivial snippet might be enough for your needs:
                >>
                >> class Foo(): pass
                >>
                >> _inst = None
                >>
                >> def fooinstance(): if not _inst: _inst = Foo() return _inst[/color][/color]

                peter> Looks like at least two errors, one being the parentheses
                peter> on the class name, and the other being a failure to flag
                peter> _inst as being global...

                True. I guess the idea got through anyway. It was late :-P.

                --
                Ville Vainio http://tinyurl.com/2prnb

                Comment

                • Aahz

                  #9
                  Re: an example of a singleton design pattern in python?

                  In article <hc63caapdui.fs f@praxis.lsil.c om>,
                  Daniel Ortmann <dortmann@lsil. com> wrote:[color=blue]
                  >
                  >Does anyone have an example of a singleton design pattern in python?[/color]

                  It appears that none of the other responses mentioned the two simplest
                  techniques:

                  * Within a single module, use a global class as your singleton object.

                  * Across modules, use a module as your singleton object.
                  --
                  Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                  A: No.
                  Q: Is top-posting okay?

                  Comment

                  • Jeff Epler

                    #10
                    Re: an example of a singleton design pattern in python?

                    On Wed, Jan 21, 2004 at 07:38:26PM -0500, Aahz wrote:[color=blue]
                    > * Within a single module, use a global class as your singleton object.[/color]

                    This is a little bit inconvenient, because you must either mark all
                    methods as staticmethod (and refer to the class by name), or you must
                    mark all methods as classmethod.

                    This metaclass can help. It makes all functions into staticmethods at
                    the time the class is created:

                    from types import FunctionType

                    class SingletonClassM eta(type):
                    def __new__(cls, name, bases, dict):
                    for k, v in dict.iteritems( ):
                    if isinstance(v, FunctionType):
                    dict[k] = classmethod(v)
                    return (super(Singleto nClassMeta, cls).
                    __new__(cls, name, bases, dict))

                    class Singleton(objec t):
                    """All methods in a Singleton object are made class methods"""
                    __metaclass__ = SingletonClassM eta

                    Probably __init__ should be made to raise an exception (RuntimeError,
                    "Singletons cannot be instantiated")) ...

                    An example:[color=blue][color=green][color=darkred]
                    >>> class C(Singleton):[/color][/color][/color]
                    .... x = []
                    .... def f(self): return self.x
                    .... def g(self, arg): self.x.append(a rg)
                    ....[color=blue][color=green][color=darkred]
                    >>> C.f()[/color][/color][/color]
                    [][color=blue][color=green][color=darkred]
                    >>> C.g(3)
                    >>> C.f()[/color][/color][/color]
                    [3]

                    Comment

                    • Aahz

                      #11
                      Re: an example of a singleton design pattern in python?

                      In article <mailman.625.10 74734700.12720. python-list@python.org >,
                      Jeff Epler <jepler@unpytho nic.net> wrote:[color=blue]
                      >On Wed, Jan 21, 2004 at 07:38:26PM -0500, Aahz wrote:[color=green]
                      >>
                      >> * Within a single module, use a global class as your singleton object.[/color]
                      >
                      >This is a little bit inconvenient, because you must either mark all
                      >methods as staticmethod (and refer to the class by name), or you must
                      >mark all methods as classmethod.[/color]

                      That's assuming one needs methods, of course; many times, the singleton
                      is just used for storing attributes.
                      --
                      Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                      A: No.
                      Q: Is top-posting okay?

                      Comment

                      Working...