Basically I've been working with classes, learning about them from the book Python Crash Course and there were these two questions I had to answer; The first one was about making a program that will identify the user whether they are an Admin or not, and if they are an Admin the code should print out that they have extra privileges and the second code is the same but I had to make an extra class called Privileges that's an attribute in the User class. But the code does the same things(), it's just much cleaner.
My first code(The one that works!):
class User:
def __init__(self, first_name, last_name, address, phone_number):
self.first_name = first_name
self.last_name = last_name
self.full_name = first_name + " " + last_name
self.address = address
self.phone_number = phone_number
self.privileges = ["can add post", "can delete post", "can ban user"]
def describe_user(self):
print("\nName: " + self.full_name.title() +
"\nAddress: " + self.address.title() +
"\nphone_number: " + str(self.phone_number))
def greet_user(self):
print("\nGreetings " + self.first_name.title() + ".")
def show_privileges(self):
print()
if self == Admin:
for privilege in self.privileges:
print(privilege)
else:
print("Sorry but you are not the Admin." +
"\nYou have limited privileges.")
Admin = User('Nebojsa', 'Kukic', 'A box under the bridge', 353838935494)
Admin.show_privileges()
user_2 = User("Siobhan", "O'Neill", "66 The Hermitage", 353831681859)
user_2.show_privileges()
Output:
can add post
can delete post
can ban user
Sorry but you are not the Admin.
You have limited privileges.
Now as you can see, you would assume that self would equal to Admin or User_2 in the conditional test but that's not the case for some odd reason for my other piece of code(The one that doesn't work :'( ):
class Privileges:
def __init__(self):
self.user_privileges = ["can add post", "can delete post", "can ban user"]
def show_privileges(self):
print()
if self == Admin:
for user_privilege in self.user_privileges:
print(_user_privilege)
else:
print("Sorry but you are not the Admin." +
"\nYou have limited privileges.")
class User:
def __init__(self, first_name, last_name, address, phone_number):
self.first_name = first_name
self.last_name = last_name
self.full_name = first_name + " " + last_name
self.address = address
self.phone_number = phone_number
self.privileges = Privileges()
def describe_user(self):
print("\nName: " + self.full_name.title() +
"\nAddress: " + self.address.title() +
"\nphone_number: " + str(self.phone_number))
def greet_user(self):
print("\nGreetings " + self.first_name.title() + ".")
Admin = User('Nebojsa', 'Kukic', 'A box under the bridge', 353838935494)
Admin.privileges.show_privileges()
user_2 = User("Siobhan", "O'Neill", "66 The Hermitage", 353831681859)
user_2.privileges.show_privileges()
Output:
Sorry but you are not the Admin.
You have limited privileges.
Sorry but you are not the Admin.
You have limited privileges.
I don't understand why the Admin instance does not return:
can add post
can delete post
can ban user
But instead it returns:
Sorry but you are not the Admin.
You have limited privileges.
I don't understand how does self not equal Admin? Sorry again about the length of the question, thanks.
This is not a good way to do this in the first place.
But anyway, self
is not equal to Admin
, which is a User
object. self
will be the Privileges
object... A better approach would be to make a boolean attribute that says whether or not there is admin privileges, something like:
class Privileges:
def __init__(self, is_admin=False):
self.user_privileges = ["can add post", "can delete post", "can ban user"]
self.is_admin = is_admin
def show_privileges(self):
print()
if self.is_admin:
for user_privilege in self.user_privileges:
print(user_privilege)
else:
print("Sorry but you are not the Admin." +
"\nYou have limited privileges.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.