title: What's the best way to check if class instance variable is set in Python?
body: <p>I have a variable which may or may not get a value in the instance:</p>
  
  <pre><code>class EC():
     __init__(self, a=False):
     ...
     if a: self.__var = ...
  </code></pre>
  
  <p>Later I want to check if the <strong>__var</strong> exists in the instance. Because prepending <strong>__</strong> to the name changes the internal name to <strong>_EC__var</strong> the checking code becomes a little bit messy:</p>
  
  <pre><code>if ''.join(['_',self.__class__.__name__,'__name']) in self.__dict__: ...
  </code></pre>
  
  <p><strong>Is code above considered normal or not? If not what are the preferred alternatives?</strong></p>
  
  <p>One option I can think of is to give <strong>__var</strong> some value anyway, for example:</p>
  
  <pre><code>_no_value = object()
  ...
     def __init__(self, a):
        self.__var = _no_value
        ...
        if a: self.__var = ...
  </code></pre>
  
  <p>So later I can compare <strong>__var</strong> to <strong>_no_value</strong> instead of a mess with internal variables.</p>