[Tutor] Scope and elegance

Kent Johnson kent37 at tds.net
Mon Jan 7 22:23:29 CET 2008


Torsten Marek wrote:

> Maybe I'm spoiled from programming too much Java in the last year, but

Hmm. Would that be
spoil 3 a: to damage seriously : ruin
or
spoil 4 b: to pamper excessively : coddle

? ;-)

> IMHO it's a good idea to put the singleton instance into the class
> itself rather than into some module.
> 
> This way, you (can) make sure that all accesses to the class really go
> to the same instance. 

This is not needed in Python, module-level variables are essentially 
singletons.

> There are, of course, many ways to do that, but I'd prefer a method on
> the class:
> 
> class Foo(object):
>     @classmethod
>     def instance(cls):
>         try:
>            return cls._inst
>         except AttributeError:
>            cls._inst = Foo()
>            return cls._inst

Yikes! Looks like 3a to me! How is this safer than having a single 
global instance?

Kent


More information about the Tutor mailing list