[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] your opinion: singleton-pattern vs. modules vs. classes



Olaf Nowacki wrote:

i'm not sure if i got this right: you
- use an "ordinary" class
- make an instance in a module and
- excess this instance only via the module?

Yes, that's essentially right.

If you can create the instance as soon as the
module is loaded, all you need is a module-level
name referring to it.

  class MyClass:
    ...

  instance = MyClass()

The advantage of accessing the instance through
a function is that you can defer creating it
until the first time it's needed, e.g.

  def get_instance():
    global instance
    if not instance:
      instance = MyClass()
    return instance

--
Greg