Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that best practice in Python 2.7 is to use <em><a href="https://www.python.org/doc/newstyle/" rel="noreferrer">new-style</a></em> classes (not needed with Python 3), i.e.</p> <pre><code>class Foo(object): ... </code></pre> <p>Also, there's a difference between an 'object' and a 'class'. To build a dictionary from an arbitrary <em>object</em>, it's sufficient to use <code>__dict__</code>. Usually, you'll declare your methods at class level and your attributes at instance level, so <code>__dict__</code> should be fine. For example:</p> <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; class A(object): ... def __init__(self): ... self.b = 1 ... self.c = 2 ... def do_nothing(self): ... pass ... &gt;&gt;&gt; a = A() &gt;&gt;&gt; a.__dict__ {'c': 2, 'b': 1} </code></pre> <p>A better approach (suggested by <a href="https://stackoverflow.com/users/409638/robert">robert</a> in comments) is the builtin <a href="https://docs.python.org/3/library/functions.html#vars" rel="noreferrer"><code>vars</code></a> function:</p> <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; vars(a) {'c': 2, 'b': 1} </code></pre> <p>Alternatively, depending on what you want to do, it might be nice to inherit from <code>dict</code>. Then your class is <em>already</em> a dictionary, and if you want you can override <code>getattr</code> and/or <code>setattr</code> to call through and set the dict. For example:</p> <pre><code>class Foo(dict): def __init__(self): pass def __getattr__(self, attr): return self[attr] # etc... </code></pre>
    singulars
    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. 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.
 

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