I am trying to learn how to tell if my code will be better off creating a class as opposed to using many functions. I am a new Python programmer trying to get the hang of when classes should be implemented.
Determines whether the username is valid or invalid:
def is_good_user(user):
is_user_good = True
if user in database:
print "Sorry! Username is taken! Create another."
is_user_good = False
if not 5 < len(user) < 25:
print "Username must be at least 6 characters!"
is_user_good = False
if is_ascii(some_string="") is not True:
print "Please create username that follows ASCII formatting."
is_user_good = False
return is_user_good
Determines whether the password is valid or invalid:
def is_good_password(password):
count_upper, count_lower = 0, 0
for characters in password:
if characters.isupper():
count_upper += 1
if characters.islower():
count_lower += 1
is_password_good = True
if len(password) <= 10:
print "Password is too weak, must be more than 10 characters long!"
is_password_good = False
if set(database).intersection(password):
print "Password Must Contain Alphanumeric Characters!"
is_password_good = False
if count_upper < 1 or count_lower < 1:
print "Password must contain at least one uppercase and one lowercase character!"
is_password_good = False
return is_password_good
These two functions are amalgamated into one function to create a username and password:
def create_user(database):
while True:
user = raw_input("Enter a New Username: ")
if is_good_user(user):
print "Great Job! Username Successfully Created! Time to Create a Password!"
break
print "That's not a good username, please try again!"
while True:
passcode = raw_input("Enter a New Password: ")
if is_good_password(passcode):
print "Welcome! Username & Password successfully created!"
break
print "That's not a good password, please try again!"
database[user] = passcode
make_digest(password=passcode, salt=None)
dump_data()
Should I add in a class for readability of my code? Being new at Python, I'd like to take up the challenge if adding a class is necessary.
1 Answer 1
It's a design choice. A class is "meant to be" a collection that encapsulates functionality and "behavior" in code. The purpose is to expose specifically an interface where a class's instances can live on their own.
A function/method is an independent piece of code that in theory "shouldn't" relate to other pieces of code outside of it.
So for the example you've described, you could create a class named "User". And user can include these functions as it's members.
This does make it more readable and easier to debug.
However do not take unrelated functions and dump them into a class just cuz!
Examples are utility type functions, they could be kept in the vicinity of each other but should not be clubbed together as a class.
An additional benefit of classes is that they can be Inherited from. So more complex classes can inherit the basic functionality and add to it, while using the features already in the base class. You can achieve a similar effect by having "higher" versions of functions that calls more basic level functions. There again at least in my design perspective, I make a class where the whole functionality will be inherited as opposed to functions that are stand-alone.