13
13
Python programmers almost never implement the Singleton Pattern
14
14
as described in the :doc: `/gang-of-four/index `,
15
15
whose Singleton class forbids normal instantiation
16
- and instead offers a class method
17
- that returns the single instance.
18
- Instead, Python lets a class offer what looks like normal instantiation
19
- that actually returns a singleton instance
20
- through a custom ``__new__() `` method.
16
+ and offers a class method that returns the single instance.
17
+ Instead, Python lets classes define a custom ``__new__() `` method
18
+ that returns a singleton instance each time the class is called.
21
19
But an even more Pythonic approach,
22
20
if your design forces you to offer global access to a singleton object,
23
21
is to use :doc: `/python/module-globals/index ` instead.
@@ -38,7 +36,7 @@ of “singleton” in Python.
38
36
The Python Tutorial itself introduces newcomers to this definition
39
37
when its chapter on `Data Structures
40
38
<https://docs.python.org/3/tutorial/datastructures.html> `_
41
- calls a one-element tuple a “singleton”,
39
+ calls a one-element tuple a “singleton”
42
40
and the word continues to be used in that sense
43
41
through the rest of Python’s documentation.
44
42
When the `Extending and Embedding <https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c >`_
@@ -59,8 +57,7 @@ of “singleton” in Python.
59
57
60
58
3. A “singleton” is a class instance that has been assigned a global name
61
59
through :doc: `/python/module-globals/index `.
62
- For example, the official Python
63
- `Programming FAQ <https://docs.python.org/3/faq/programming.html >`_
60
+ For example, the official Python Programming FAQ
64
61
answers the question
65
62
`“How do I share global variables across modules?”
66
63
<https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules> `_
@@ -113,11 +110,10 @@ But their classes were not callable:
113
110
>>> type(Ellipsis)()
114
111
TypeError: cannot create 'ellipsis' instances
115
112
116
- But in Python 3, the classes were upgraded to use the Singleton Pattern:
113
+ In Python 3, however , the classes were upgraded to use the Singleton Pattern:
117
114
118
115
>>> NoneType = type (None )
119
- >>> result = NoneType()
120
- >>> print (result)
116
+ >>> print (NoneType())
121
117
None
122
118
>>> type (Ellipsis )()
123
119
Ellipsis
@@ -129,7 +125,7 @@ In most Python projects these classes are never called
129
125
and the benefit remains purely theoretical.
130
126
When Python programmers need the ``None `` object
131
127
they use :doc: `/python/module-globals/index `
132
- by simply typing its name.
128
+ and simply type its name.
133
129
134
130
The Gang of Four’s implementation
135
131
=================================
@@ -154,27 +150,27 @@ what were their options for offering singleton objects?
154
150
because it did not work particularly well
155
151
in early versions of the C++ language.
156
152
There, global names all shared a single crowded global namespace,
157
- and elaborate naming conventions were necessary
153
+ so elaborate naming conventions were necessary
158
154
to prevent names from different libraries from colliding.
159
155
So the Gang judged that adding both a class and its singleton instance
160
156
to the global namespace was excessive.
161
157
And since C++ programmers couldn’t control the order
162
158
in which global objects were initialized,
163
159
no global object could depend on being able to call any other,
164
- so the responsibility for initializing each global
165
- would often have fallen on client code.
160
+ so the responsibility of initializing globals
161
+ often fell on client code.
166
162
167
- 2. There was no way to override the meaning of ``new `` in C++
163
+ 2. There was no way to override the meaning of ``new `` in C++,
168
164
so an alternative syntax was necessary
169
165
if all clients were to receive the same object.
170
166
It was, though, at least possible to make it a compile-time error
171
- for client code to call ``new ``,
167
+ for client code to call ``new ``
172
168
by marking the class constructor as either ``protected `` or ``private ``.
173
169
174
170
3. So the Gang of Four pivoted to a class method
175
171
that would return the class’s singleton object.
176
172
Unlike a global function,
177
- a class method avoided adding yet another name to the C++ global namespace,
173
+ a class method avoided adding yet another name to the global namespace,
178
174
and unlike a static method,
179
175
it could support subclasses that were singletons as well.
180
176
@@ -277,7 +273,7 @@ because Python lacks a ``new`` keyword
277
273
that forces a new object to be created.
278
274
Instead, objects are created by invoking a callable,
279
275
which imposes no syntactic limitation
280
- on what operation the callable performs::
276
+ on what operation the callable really performs::
281
277
282
278
log = Logger()
283
279
@@ -290,7 +286,7 @@ The Web is replete with Singleton Pattern recipes featuring ``__new__()``
290
286
that each propose a more or less complicated mechanism
291
287
for working around the method’s biggest quirk:
292
288
the fact that ``__init__() `` always gets called on the return value,
293
- whether the object that’s been returned is new or not.
289
+ whether the object that’s being returned is new or not.
294
290
To make my own example simple,
295
291
I will simply not define an ``__init__() `` method
296
292
and thus avoid having to work around it:
@@ -367,7 +363,7 @@ it’s not as easy to dismiss the pattern when it’s built atop ``__new__()``
367
363
after all, singletons were part of the reason
368
364
the ``__new__() `` dunder method was introduced!
369
365
370
- But the Singleton Pattern in Python always seems to have drawbacks.
366
+ But the Singleton Pattern in Python does suffer from several drawbacks.
371
367
372
368
A first objection is that the Singleton Pattern’s implementation
373
369
is difficult for many Python programmers to read.
@@ -391,8 +387,8 @@ that :doc:`/python/module-globals/index` does not.
391
387
Offering a global object still leaves code
392
388
free to create other instances of the class —
393
389
which can be particularly helpful for tests,
394
- that otherwise wind up coupled together
395
- through the single global object they are forced to share .
390
+ letting them each test a completely separate object
391
+ without needing to reset a shared object back to a known good state .
396
392
But the Singleton Pattern makes additional instances impossible.
397
393
(Unless the caller is willing to stoop to monkey patching;
398
394
or temporarily modifying ``_instance `` to subvert the logic in ``__new__() ``;
@@ -405,11 +401,14 @@ would be an existing class that,
405
401
because of a new requirement,
406
402
will now operate best as a single instance.
407
403
If it’s not possible to migrate all client code to using a global object,
408
- then using the Singeton Pattern would be a natural approach
409
- to pivoting to a singleton design while preserving the old syntax.
404
+ then the Singeton Pattern would be a natural approach
405
+ to pivoting to a singleton while preserving the old syntax.
410
406
411
407
But, otherwise, the pattern is best avoided
412
- in favor of :doc: `/python/module-globals/index `.
408
+ in favor of following the advice
409
+ of the `official Python FAQ
410
+ <https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules> `_
411
+ and using the :doc: `/python/module-globals/index `.
413
412
414
413
.. See also
415
414
0 commit comments