- #36
- 2,168
- 193
I did this, it also seems nice. I did not much pay attention at first because I wanted Admin and User to be separate classes. But this is also good.PeterDonis said:That's not what your code posted in the OP is doing. Your code posted in the OP is assigning a new instance of the Admin class to the admin instance attribute of each new instance of the User class. Since each new instance of the Admin class has exactly the same attributes, it's impossible to use that to distinguish between admin and non-admin users, so your code is not doing what you intend.
What it looks like you are trying to do is to distinguish admin users from other users by their names (i.e., users with particular names are admins, other users are not). The name is an attribute of the User class, so you don't need a separate Admin class to distinguish admin from non-admin users; you just need an attribute or property on each instance of the User class that says whether that user is an admin, based on their name (or whatever other criterion you want). One example of how to do this would be to add the following to your User class:
Python:@property def is_admin(self): return self.first_name == "Arman" and self.last_name == "Cam"
Then you can use this property in the show_privileges method of the User class, for example:
Python:def show_privileges(self): """ Showing the privileges of the admin """ if self.is_admin: print(self.privileges) else: print("You have no privilege")
And for this to work, you would need to replace the lineself.admin = Admin(privileges='')
in the User class constructor withself.privileges = ''
(or you could pass the privileges in as a parameter).