<p>While answering <a href="https://stackoverflow.com/questions/68645/python-static-variable#81002">Static class variables in Python</a> </p>

<p>I noticed that PythonWin PyWin32 build 209.2 interpreter seems to evaluate twice?</p>

<pre><code>PythonWin 2.5 (r25:51908, Mar  9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
&gt;&gt;&gt; class X:
...     l = []
...     def __init__(self):
...         self.__class__.l.append(1)
...         
&gt;&gt;&gt; X().l
[1, 1]
&gt;&gt;&gt; 
</code></pre>

<p>while the python interpreter does the right thing</p>

<pre><code>C:\&gt;python
ActivePython 2.5.0.0 (ActiveState Software Inc.) based on
Python 2.5 (r25:51908, Mar  9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; class X:
...   l = []
...   def __init__(self):
...     self.__class__.l.append(1)
...
&gt;&gt;&gt; X().l
[1]
&gt;&gt;&gt;
</code></pre>

[{'answer_id': 81274, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 3, 'selected': True, 'text': "<p>My guess is as follows.  The PythonWin editor offers autocomplete for an object, i.e. when you type <code>myobject.</code> it offers a little popup of all the availble method names.  So I think when you type <code>X().</code> it's creating an instance of <code>X</code> in the background and doing a <code>dir</code> or similar to find out the attributes of the object.</p>\n\n<p>So the constructor is only being run <em>once for each object</em> but to give you the interactivity it's creating objects silently in the background without telling you about it.</p>\n"}, {'answer_id': 81751, 'author': 'S.Lott', 'author_id': 10661, 'author_profile': 'https://Stackoverflow.com/users/10661', 'pm_score': 1, 'selected': False, 'text': "<p>Two small additional points.</p>\n\n<p>First, <code>self.__class__.l.append(1)</code> isn't really sensible.</p>\n\n<p>Just say <code>self.l.append(1)</code>.  Python searches the instance before it searches the class for the reference.</p>\n\n<p>More importantly, class-level variables are rarely useful.  Class-level constants are sometimes sensible, but even then, they're hard to justify. </p>\n\n<p>In C++ and Java, class-level ('<code>static</code>') variables seem handy, but don't do much of value.  They're hard to teach to n00bz -- often wasting lots of classroom time on minutia -- and they aren't very practical.  If you want to know all instances of an X that was created, it's probably better to create an XFactory class that doesn't rely on class variables.</p>\n\n<pre><code>class XFactory( object ):\n    def __init__( self ):\n        self.listOfX= []\n    def makeX( self, *args, **kw ):\n        newX= X(*args,**kw)\n        self.listOfX.append(newX)\n        return newX\n</code></pre>\n\n<p>No class-level variable anomalies.  And, it doesn't conflate the X's with the collection of X's.  In the long run, I find it confusing when a class is both some thing and also some collection of things.</p>\n\n<p>Simpler is better than Complex.</p>\n"}, {'answer_id': 84248, 'author': 'Bill Barksdale', 'author_id': 16113, 'author_profile': 'https://Stackoverflow.com/users/16113', 'pm_score': 2, 'selected': False, 'text': "<p>Dave Webb is correct, and you can see this by adding a print statement:</p>\n\n<pre><code>&gt;&gt;&gt; class X:\n...     l = []\n...     def __init__(self):\n...             print 'inited'\n...             self.__class__.l.append(1)\n...             \n</code></pre>\n\n<p>Then as soon as you type the period in <code>X().</code> it prints <code>inited</code> prior to offering you the completion popup.</p>\n"}]
['https://Stackoverflow.com/questions/81191', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14351/']