Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to adapt the Singleton pattern? (Deprecation warning)
    primarykey
    data
    text
    <p>Few years ago I found an implementation of the Singleton pattern in Python by <a href="http://web.archive.org/web/20090619190842/http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html" rel="nofollow noreferrer">Duncan Booth</a>:</p> <pre><code>class Singleton(object): """ Singleton class by Duncan Booth. Multiple object variables refers to the same object. http://web.archive.org/web/20090619190842/http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html#singleton-and-the-borg """ _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__( cls, *args, **kwargs) return cls._instance </code></pre> <p>The same approach is also described in question "<a href="https://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python/1810391#1810391">Is there a simple, elegant way to define Singletons in Python?</a>"</p> <p>I use the Singleton via sub-classing:<br> <code>class Settings(Singleton)</code><br> <code>class Debug(Singleton)</code> </p> <p>Recently I made some changes to the program and got this warning:</p> <pre><code>/media/KINGSTON/Sumid/src/miscutil.py:39: DeprecationWarning: object.__new__() takes no parameters cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) </code></pre> <p>I found <a href="http://mail.python.org/pipermail/python-dev/2008-February/076854.html" rel="nofollow noreferrer">explanation</a> (by Guido) about <code>__new__</code> deprecation which says that the parameters are not used at all. Passing an argument which is not needed can be symptom of a bug.</p> <p>So I decided to clear the parameters:</p> <pre><code>class Singleton(object): _instance = None def __new__(cls): if not cls._instance: cls._instance = super(Singleton, cls).__new__() return cls._instance </code></pre> <p>Which resulted in following exception:</p> <pre><code>Traceback (most recent call last): File "sumid.py", line 1168, in &lt;module&gt; settings = Settings() File "/media/KINGSTON/Sumid/src/miscutil.py", line 45, in __new__ cls._instance = super(Singleton, cls).__new__() TypeError: object.__new__(): not enough arguments </code></pre> <p>When I modify the line to <code>cls._instance = super(Singleton, cls).__new__(cls)</code>, I'll get:</p> <pre><code>Traceback (most recent call last): File "sumid.py", line 1174, in &lt;module&gt; debug = Debug(settings) TypeError: __new__() takes exactly 1 argument (2 given) </code></pre> <p>Gimmel suggest another <a href="https://stackoverflow.com/questions/1590477/python-deprecation-warnings-with-monostate-new-can-someone-explain-why/1590586#1590586">solution</a>: <code>_instance = type.__new__(cls)</code>. For me it breaks the inheritance:</p> <pre><code>Traceback (most recent call last): File "sumid.py", line 1168, in &lt;module&gt; settings = Settings() File "/media/KINGSTON/Sumid/src/miscutil.py", line 40, in __new__ _instance = type.__new__(cls) TypeError: type.__new__(Settings): Settings is not a subtype of type </code></pre> <p>The same <a href="http://freshfoo.com/blog/object__init__takes_no_parameters" rel="nofollow noreferrer">problem</a> has also Menno Smits. But I don't understand the <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=281127" rel="nofollow noreferrer">solution</a> suggested. Moreover I have no multiple inheritance in relevant code.</p> <p>I didn't try the another <a href="https://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python/33201#33201">example</a> in "Is there a simple, elegant way to define Singletons in Python?", but at a glance it probably will have the same problem.</p> <p>I use the Singleton pattern in a program and I don't want to rewrite it completely just because one warning. Thus following answers wont help me: </p> <ul> <li>Singleton is wrong. Don't use the Singleton at all. </li> <li>Use Borg instead, it's more pythonic. </li> <li>Use module instead of a class. </li> </ul> <p>To conclude I'll repeat the question:<br> How to <strong>adapt the Singleton</strong> pattern with consideration of the <strong>deprecation warning</strong> with the <strong>minimal impact</strong> to existing code?</p> <p><strong>Edit:</strong> Line <code>cls._instance = object.__new__(cls)</code> raises the TypeError when the Child's init takes an argument:</p> <pre><code>class Child(Singleton): def __init__(self,param=None): print(param) print("Doing another stuff.") ch = Child("Some stuff") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload