Assign a class to itself in python
<p>I just want to ask if I can assign an instance of a class to itself in a method.</p>

<p>For example, is the following valid python code?</p>

<pre><code>class O(object):
  def __init__(self,value):
    self.value = value
  def do_something(self):
    self = O(1)
</code></pre>

<p>Does this lead to any unexpected behaviour?</p>

<p>Obviously, the code can be run. But when I do</p>

<pre><code>A = O(2)
A.do_something()
A.value 
</code></pre>

<p>the output is <code>2</code>, when I expect it to be <code>1</code>.</p>

<p><code>self</code> is a local variable. So, it will make your code confusing in that method, and will prevent modifications on (or calls to) the original self object. </p>

python class oop