Your code is out of a context so is not obvious the right choice. Following some tips:
Don't use
NameErrorexception, it is only used when a name, as the exception itself said, is not found in the local or global scope, useValueErrororTypeErrorif the exception concerns the value or the type of the parameter;Don't print error messages. Raise meaningful exceptions with a meaningful error message:
raise ValueError("password must be longer than 6 characters")Returning a value from a setter is meaningless while assignment is not an expression, i.e. you cannot check the value of an assignment:
if (user.password = 'short'): ...Just raise an exception in the setter and let the code that set the property handle it.
Example:
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) < self.minlen:
raise ValueError("password must be at least %d character len" % \
self.minlen)
self._password = value
Look also at this forms handling library, there the validators , here an example, 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.
Answer from mg. on Stack OverflowYour code is out of a context so is not obvious the right choice. Following some tips:
Don't use
NameErrorexception, it is only used when a name, as the exception itself said, is not found in the local or global scope, useValueErrororTypeErrorif the exception concerns the value or the type of the parameter;Don't print error messages. Raise meaningful exceptions with a meaningful error message:
raise ValueError("password must be longer than 6 characters")Returning a value from a setter is meaningless while assignment is not an expression, i.e. you cannot check the value of an assignment:
if (user.password = 'short'): ...Just raise an exception in the setter and let the code that set the property handle it.
Example:
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) < self.minlen:
raise ValueError("password must be at least %d character len" % \
self.minlen)
self._password = value
Look also at this forms handling library, there the validators , here an example, 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.
The standard way of signalling an error in python is to raise an exception and let the calling code handle it. Either let the NameError & TypeError carry on upwards, or catch them and raise an InvalidPassword exception that you define.
While it is possible to return a success/fail flag or error code from the function as you have done, it is not recommended - it is easy for the caller to forget to check the return value and have errors get lost. Besides you are returning a value from a property setter - this is meaningless in Python since assignments are not expressions and cannot return a value.
You should also never print a message for the user in your exception handling - what if you later want to use the function or class in a GUI program? In that case your print statement will have nowhere to print to. Logging an error to a logfile (using Python's logging module) is often helpful for debugging though.