Title: Why won't my Privileges class that's connected via an attribute in my User class work properly?
Tags: python;python-3.x;class;attributes
Question: 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.
Comment: @buhtz I’ll definitely keep that in mind for next time, thanks.

Comment: To much code. Please provide a minimal working example.

Comment: Not next time. Now.

Here is the accepted answer: 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.")
Comment for this answer: One more thing, how would I be able to make the is_admin be True so print(user_privilege) would work?


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:04/08/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><python-3.x><class><attributes>', 'Title': "Why won't my Privileges class that's connected via an attribute in my User class work properly?", 'ViewCount': 54, 'id': 49722115, 'user_AboutMe': '', 'user_AccountId': 11634502.0, 'user_DisplayName': 'Neb', 'user_Reputation': 31.0, 'user_Views': 6.0, 'user_id': 8521386.0}}