Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could implement this nicely with <a href="http://docs.python.org/library/collections.html#collections.namedtuple" rel="nofollow">namedtuple</a>. For example, you could create a Widget namedtuple with default values:</p> <pre><code>&gt;&gt;&gt; from collections import namedtuple &gt;&gt;&gt; _Widget = namedtuple("Widget", "propertyA propertyB propertyC propertyD propertyE propertyF propertyG propertyH propertyI") &gt;&gt;&gt; DefaultWidget = _Widget(None, 55, "Default", None, None, None, None, None, None) &gt;&gt;&gt; DefaultWidget Widget(propertyA=None, propertyB=55, propertyC='Default', propertyD=None, propertyE=None, propertyF=None, propertyG=None, propertyH=None, propertyI=None) </code></pre> <p>Then, you can have a function called Widget that initializes the properties:</p> <pre><code>def Widget(propertyA, **kwargs): return DefaultWidget._replace(propertyA=propertyA, **kwargs) </code></pre> <p>Then you can use it like this:</p> <pre><code>&gt;&gt;&gt; Widget("test", propertyE=17) Widget(propertyA='test', propertyB=55, propertyC='Default', propertyD=None, propertyE=17, propertyF=None, propertyG=None, propertyH=None, propertyI=None) </code></pre> <p>Note that if you try to omit the required propertyA:</p> <pre><code>&gt;&gt;&gt; Widget() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: Widget() takes exactly 1 argument (0 given) </code></pre> <p>or if you give a property that doesn't exist:</p> <pre><code>&gt;&gt;&gt; Widget("test", propertyZ="test2") Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "&lt;stdin&gt;", line 2, in Widget File "&lt;string&gt;", line 32, in _replace ValueError: Got unexpected field names: ['propertyZ'] </code></pre> <p>it handles it in a nice way. I think using namedtuple would get rid of your drawbacks about using a dictionary.</p>
 

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