Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Blair Conrad said static variables declared inside the class definition, but not inside a method are class or "static" variables:</p> <pre><code>&gt;&gt;&gt; class Test(object): ... i = 3 ... &gt;&gt;&gt; Test.i 3 </code></pre> <p>There are a few gotcha's here. Carrying on from the example above:</p> <pre><code>&gt;&gt;&gt; t = Test() &gt;&gt;&gt; t.i # static variable accessed via instance 3 &gt;&gt;&gt; t.i = 5 # but if we assign to the instance ... &gt;&gt;&gt; Test.i # we have not changed the static variable 3 &gt;&gt;&gt; t.i # we have overwritten Test.i on t by creating a new attribute t.i 5 &gt;&gt;&gt; Test.i = 6 # to change the static variable we do it by assigning to the class &gt;&gt;&gt; t.i 5 &gt;&gt;&gt; Test.i 6 &gt;&gt;&gt; u = Test() &gt;&gt;&gt; u.i 6 # changes to t do not affect new instances of Test # Namespaces are one honking great idea -- let's do more of those! &gt;&gt;&gt; Test.__dict__ {'i': 6, ...} &gt;&gt;&gt; t.__dict__ {'i': 5} &gt;&gt;&gt; u.__dict__ {} </code></pre> <p>Notice how the instance variable <code>t.i</code> got out of sync with the "static" class variable when the attribute <code>i</code> was set directly on <code>t</code>. This is because <code>i</code> was re-bound within the <code>t</code> namespace, which is distinct from the <code>Test</code> namespace. If you want to change the value of a "static" variable, you must change it within the scope (or object) where it was originally defined. I put "static" in quotes because Python does not really have static variables in the sense that C++ and Java do.</p> <p>Although it doesn't say anything specific about static variables or methods, the <a href="http://docs.python.org/tut/" rel="noreferrer">Python tutorial</a> has some relevant information on <a href="https://docs.python.org/2/tutorial/classes.html" rel="noreferrer">classes and class objects</a>. </p> <p>@Steve Johnson also answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.</p> <pre><code>class Test(object): @staticmethod def f(arg1, arg2, ...): ... </code></pre> <p>@beid also mentioned classmethod, which is similar to staticmethod. A classmethod's first argument is the class object. Example:</p> <pre><code>class Test(object): i = 3 # class (or static) variable @classmethod def g(cls, arg): # here we can use 'cls' instead of the class name (Test) if arg &gt; cls.i: cls.i = arg # would the the same as Test.i = arg1 </code></pre> <p><a href="https://i.stack.imgur.com/xqnxe.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/xqnxe.jpg" alt="Pictorial Representation Of Above Example"></a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload