I confused myself typing this title, so please let me know if it requires some clarification. I'm working my way through Python Crash Course, by Eric Matthes and one of the exercises brought up a question that I haven't been able to find an answer for. Also, apparently, I can't figure out how to properly format the following code, sorry. The code for the exercise in question follows:
class User():
"""Store info about a user profile"""
def __init__(self, first_name, last_name, email, username):
"""Init common profile info"""
self.first_name = first_name
self.last_name = last_name
self.email = email
self.username = username
self.login_attempts = 0
def describe_user(self):
"""Print users info"""
print("User info summary: \n\t" +
self.first_name.title() + " " +
self.last_name.title() + "\n\t" +
self.username + "\n\t" +
self.email
)
def greet_user(self):
"""Print a short greeting"""
print("Welcome, " + self.first_name.title() + "!")
def increment_login_attempts(self):
"""Increments the number of login attempts by 1"""
self.login_attempts += 1
def reset_login_attempts(self):
"""Rest value of login_attempts to 0"""
self.login_attempts = 0
class Privileges():
"""Represents a list of privileges"""
def __init__(self, privileges=[]):
"""Initialize attributes for Privileges"""
self.privileges = privileges
def show_privileges(self):
"""Print the list of privileges for this admin"""
for item in self.privileges:
print(item.title())
class Admin(User):
"""Model an administrative user"""
def __init__(self, first_name, last_name, username, email):
"""Initialize attributes of Admin"""
super().__init__(first_name, last_name, username, email)
self.privileges = Privileges(['can add user', 'can block user'])
bbob = Admin('billy', 'bob', 'bbob', 'blah')
bbob.privileges.show_privileges()
OK so, I'd like to be able to provide the list of privileges to the instance of class Privileges
that's being created inside of Admin
, at the time I'm creating the instance of Admin
. Does this all make sense? Every time I type it out I get confused. It seems to me, that in a potential real-world application, it would be unreasonable to provide that list in the class definition, as it is in Admin
's __init__
method. It should be able to be passed to the class as an argument or something, right?
Thanks, all, for your time reading this. Please let me know if this didn't make any sense at all.