Static class property pointing to special instance of the same class in Python
<p>Coming from cpp/c#, how does one refer to the same class in the class body in Python:</p>

<pre><code>class Foo(object):
    ANSWER = Foo(42)
    FAIL = Foo(-1)

    def __init__(self, value):
        self._v = value
</code></pre>

<p>When I try to use this code, I get "name 'Foo' is not defined" exception in a line trying to instantiate the ANSWER instance.</p>

<p>The name <code>Foo</code> is not set until the full class body has been executed. The only way you can do what you want is to add attributes to the class <em>after</em> the <code>class</code> statement has completed:</p>

<pre><code>class Foo(object):    
    def __init__(self, value):
        self._v = value

Foo.ANSWER = Foo(42)
Foo.FAIL = Foo(-1)
</code></pre>

<p>It sounds like you are re-inventing Python's <a href="https://docs.python.org/3/library/enum.html" rel="nofollow"><code>enum</code> module</a>; it lets you define a class with constants that are really instances of that class:</p>

<pre><code> from enum import Enum

 class Foo(Enum):
     ANSWER = 42
     FAIL = -1
</code></pre>

<p>After that <code>class</code> statement has run, <code>Foo.ANSWER</code> is an instance of <code>Foo</code> with a <code>.value</code> attribute set to <code>42</code>.</p>

python class