[Tutor] singleton pattern

D-Man dsh8290@rit.edu
Wed, 16 May 2001 11:57:24 -0400


On Wed, May 16, 2001 at 11:50:30AM -0400, Michael P. Reilly wrote:
| D-Man wrote
| > ######## Factory.py ##########
| > 
| > class _Factory :
| >     def create_object( self ) :
| >         pass
| > 
| > _instance = None
| > 
| > def get_instance() :
| >     if _instance is None :
| >         _instance = _Factory()
| >     return _instance
| 
| Just to be safe, you'll want to qualify the _instance as a global since
| you have an assignment in the function and it will be taken as a local
| (or you'll get an UnboundLocalError exception in later releases).

Uhh...right.  Doh!  I forgot the 'global' declaration.  In current releases,
the effect is to get a new _Factory instance for each invocation of
the function.  Not quite what was intended ;-).

-D