title: I want a class' __init__ to add the object to a list, but I want to prevent redundancy and "if self not in objectlist" isn't working
body: <p>I'm writing a script that checks multiple sources for weather data, then parses them for an some scripts on Scripting Layer for Android. The google API stopped working, so this is a hacked together replacement for the old weather module. </p>
  
  <p>I made a class called "weatherdata", and I want to have all instances of the class add themselves to a list called "weatherobjects", for shenanigans like this:</p>
  
  <pre><code>    for source in weatherobjects:
          source.check()
  </code></pre>
  
  <p>Here's the catch: each time the function that fetches the weather is called, it causes the objects to run their <code>__init__</code> method (I think this is technically called a constructor method?) without destroying the objects, or clearing the list. This is intentional. The problem comes when the function is called more than once in the lifetime of the module, and and the objects get redundantly added to the list. This seems like a potential source of a memory leak.</p>
  
  <p>Here's the <code>__init__</code> method:</p>
  
  <pre><code>class weatherdata():
      def __init__(self, url, unit = 'f'):
          self.url = url
          self.unit = unit
          print(self) #debug statement, please ignore
          if self not in weatherobjects:
              weatherobjects.append(self)
          if self.url.count("yahoo"):
              self.yahoo = True
          else:
              self.yahoo = False
          self.check()
  </code></pre>
  
  <p>And the troublesome function:</p>
  
  <pre><code>def fetch_weather(location=98661, hl='', weatherobjects= []):
      yahoo = weatherdata(yahoo_url, 'f')
      wunderground = weatherdata(wunderground_url, 'f')
      data = {}
      data['city'] = 'Vancouver'
      data['temperature'] = wunderground.temp
      data['conditions'] = 'foo'
      return data
  </code></pre>
  
  <p>Here's some shell output for context:</p>
  
  <pre><code>&gt;&gt;&gt; weatherobjects
  [&lt;__main__.weatherdata object at 0x01F8BDF0&gt;, &lt;__main__.weatherdata object at 0x02035B70&gt;]
  &gt;&gt;&gt; for i in range(3):
  ...     fetch_weather()
  ...
  {'city': 'Vancouver', 'conditions': 'foo', 'temperature': '66.7'}
  {'city': 'Vancouver', 'conditions': 'foo', 'temperature': '66.7'}
  {'city': 'Vancouver', 'conditions': 'foo', 'temperature': '66.7'}
  &gt;&gt;&gt; weatherobjects
  [&lt;__main__.weatherdata object at 0x01F8BDF0&gt;, &lt;__main__.weatherdata object at 0x02035B70&gt;, &lt;__main__.weatherdata object at 0x01FA2E10&gt;, &lt;__main__.weatherdata object at 0x01FA2FB0&gt;, &lt;__main__.weatherdata object at 0x02035C30&gt;, &lt;__main__.weatherdata object at 0x02035E10&gt;, &lt;__main__.weatherdata object at 0x02035DF0&gt;, &lt;__main__.weatherdata object at 0x02035D10&gt;]
  &gt;&gt;&gt; len(weatherobjects)
  8
  </code></pre>
  
  <p>As you can see, there's a lot of redundancy in the list. Is it possible to do this in the <code>__init__</code> method? Or do I need to have a main function do something like <code>weatherobjects.append(foo)</code>?</p>