Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Those are <em>class</em> attributes, not instance attributes. Class attributes are shared between instances. This is also how instances inherit the methods of their class. Instance attributes with the same name shadow class attributes, so you can set <code>numbers</code> on the instance to give the instance it's own copy.</p> <p>You can use <code>dir()</code> on an instance to list a combined view of instance and class attribute names:</p> <pre><code>&gt;&gt;&gt; inst = SomeClass() &gt;&gt;&gt; dir(inst) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'numbers'] </code></pre> <p>You can use <a href="http://docs.python.org/2/library/inspect.html#inspect.getmembers" rel="nofollow"><code>inspect.getmembers()</code></a> to filter on the type of attribute.</p> <p>Another option is to list the <em>class</em> attributes with <code>__dict__</code>:</p> <pre><code>&gt;&gt;&gt; vars(type(inst)) dict_proxy({'__dict__': &lt;attribute '__dict__' of 'SomeClass' objects&gt;, '__module__': '__main__', '__weakref__': &lt;attribute '__weakref__' of 'SomeClass' objects&gt;, 'numbers': [1, 3, 5, 7, 9], '__doc__': None}) &gt;&gt;&gt; {k: v for k, v in vars(type(inst)).iteritems() if not k.startswith('__') and not inspect.isfunction(v)} {'numbers': [1, 3, 5, 7, 9]} </code></pre> <p>From the <a href="http://docs.python.org/3/reference/compound_stmts.html#class-definitions" rel="nofollow">Class definitions documentation</a>:</p> <blockquote> <p>Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with <code>self.name = value</code>. Both class and instance attributes are accessible through the notation “<code>self.name</code>”, and an instance attribute hides a class attribute with the same name when accessed in this way.</p> </blockquote>
 

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