What is the proper way to do error-checking in a class? Raising exceptions? Setting an instance variable dictionary "errors" that contains all the errors and returning it?
Is it bad to print errors from a class? Do I have to return False if I'm raising an exception?
Just want to make sure that I'm doing things right. Below is some sample code:
@property def password(self): return self._password @password.setter def password(self,password): # Check that password has been completed try: # Check that password has a length of 6 characters if (len(password) < 6): raise NameError('Your password must be greater \ than 6 characters') except NameError: print 'Please choose a password' return False except TypeError: print 'Please choose a password' return False #Set the password self._password = password #Encrypt the password password_md5 = md5.new() password_md5.update(password) self._password_md5 = password_md5.hexdigest()
As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.
Here is a simple example to handle ValueError exception using try-except block. import math x = int(input('Please enter a positive number:\n')) try: print(f'Square Root of {x} is {math. sqrt(x)}') except ValueError as ve: print(f'You entered {x}, which is not a positive number.
The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.
Your code is out of a context so is not obvious the right choice. Following some tips:
Don't use NameError
exception, it is only used when a name, as the exception itself said, is not found in the local or global scope, use ValueError
or TypeError
if 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With