How to mock a singleton class method
<p>Suppose we have the following structure:</p>

<pre><code>class A():
    class __A():
        def __to_be_mocked(self):
            #something here
    def __init__(self):
        with A.lock:
            if not A.instance:
                A.instance = A.__A()

    def __getattr__(self,name):
        return getattr(self.instance,name)
</code></pre>

<p>Now we want to mock the function <code>__to_be_mocked</code>.How can we mock it as the target accepted by <code>mock.patch.object</code> is <code>package.module.ClassName</code>.I have tried all methods like </p>

<pre><code>target = A.__A
target = A.___A
</code></pre>

<p>and many more.</p>

<p>EDIT:</p>

<p>I solved it using </p>

<pre><code>target=A._A__A and attribute as '_A__to_be_mocked`
</code></pre>

<p>Now the question is <code>__to_be_mocked</code> is inside <code>__A</code> so shouldn't it be <code>___A__to_be_mocked</code> .</p>

<p>Is it because of <code>setattribute</code> in <code>A</code> or <code>__init__</code> in A?</p>