How do constants work in an if block?
<p>I am new to python. I want to learn how to make and use "constants". Here is my code:</p>

<pre><code>class Constantine(object):
    ONE = 1
    TWO = 2

    def test(self, code):
        if code not in(self.ONE, self.TWO):
            print("safe")
        else:
            print("not safe")

keeanu = Constantine()
keeanu.test(1)
</code></pre>

<p>I expect the code to print safe. But, it prints not safe. Why ?</p>

<p>You are testing whether <code>code</code> is <strong><em>not</em></strong> in <code>(self.ONE, self.TWO)</code>. If it <strong><em>is</em></strong> found, it will print <code>not safe</code>, which it does.</p>

<p>The reason it is found is because the interpreter first looks up <code>self.ONE</code> and <code>self.TWO</code> as instance variables. If they are not found at that level, the interpreter will attempt to resolve them as class variables. This succeeds for your code.</p>

python