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()
passwordin that code. (the first function, the second function, and the argument to the second function.) pyflakes will be your friend.property()function. Look at the code example in the docs that uses.setterdecorator. Inside the setter methodpasswordis a local variable (parameter). Other names are in a different namespace (they belong to the class).