Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is out of a context so is not obvious the right choice. Following some tips:</p> <ul> <li><p>Don't use <code>NameError</code> exception, it is only used when a name, as the exception itself said, is not found in the local or global scope, use <code>ValueError</code> or <code>TypeError</code> if the exception concerns the value or the type of the parameter;</p></li> <li><p>Don't print error messages. Raise meaningful exceptions with a meaningful error message:</p> <pre><code>raise ValueError("password must be longer than 6 characters") </code></pre></li> <li><p>Returning a value from a setter is meaningless while assignment is not an expression, i.e. you cannot check the value of an assignment:</p> <pre><code>if (user.password = 'short'): ... </code></pre></li> <li><p>Just raise an exception in the setter and let the code that set the property handle it.</p></li> </ul> <p>Example:</p> <pre><code>class Test: minlen = 6 @property def password(self): return self._password @password.setter def password(self, value): if not isinstance(value, basestring): raise TypeError("password must be a string") if len(value) &lt; self.minlen: raise ValueError("password must be at least %d character len" % \ self.minlen) self._password = value </code></pre> <p>Look also at <a href="http://forms-project.pollenation.net/cgi-bin/trac.cgi/browser/trunk/formal/types.py#L16" rel="noreferrer">this forms handling library</a>, there the validators , <a href="http://forms-project.pollenation.net/cgi-bin/trac.cgi/browser/trunk/formal/validation.py#L60" rel="noreferrer">here an example</a>, are entities in their own: they can be set dynamically with higher control and less coupled code, but maybe this is much more than you need.</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