- #1
- 2,168
- 193
I am pretty new to OOP in python, and just for fun, I have created a program that distincts an admin from the user.
Now, I want to modify the `"show_privileges`" method in the `"Admin Class" such that
But in this case ofc the code gives me an error. Since "self.first_name`" is not defined in the ` "Admin" subclass.
In order to make things work I might create a subclass, but then I have to write something like
Which I do now want. I want ` "Admin" ` to be an attribute in `"User` Class".
So, how can I add `self.first_name` in `Admin` class without using subclass method?
Also since User have lots of arguments, is there a shortcut syntax for passing all arguments of `User` in other functions?
Note: By subclass method I mean that `Admin(User)`
Python:
class User():
def __init__(self, first_name, last_name, gender, age, country):
"""
Description of the user
"""
self.first_name = first_name
self.last_name = last_name
self.gender = gender
self.age = age
self.country = country
self.login_attempts = 0
self.admin = Admin(privileges='')
def describe_user(self):
"""
Printing the users information, if the user is admin do not give any information
"""
if self.first_name == "Arman" and self.last_name == "Cam":
print("This information is classified")
else:
print("First Name: ", self.first_name)
print("Last Name:", self.last_name)
print("Gender:", self.gender)
print("Age:", self.age)
print("Country:", self.country)
def greet_user(self):
"""
Greeting the users
"""
if self.first_name == "Arman" and self.last_name == "Cam":
print("Hello Admin!\nWelcome to the AQT Server")
else:
print("Hello", self.first_name, "!\nWelcome to the AQT Server\n") class Admin():
def __init__(self, privileges=''):
self.privileges = privileges
def show_privileges(self):
"""
Showing the privileges of the admin
"""
print(self.privileges)
def add_privileges(self, new_privilege):
"""
Adding a new privilege if needed
"""
self.privileges += new_privilege user1 = User("Arman", "Cam", "Male", 20, "Canada")
user1.greet_user()
user1.describe_user()
user1.admin.add_privileges("ban user")
user1.admin.show_privileges()
Python:
def show_privileges(self):
"""
Showing the privileges of the admin
"""
if self.first_name == "Arman" and self.last_name == "Cam":
print(self.privileges)
else:
print("You have no privilege")
But in this case ofc the code gives me an error. Since "self.first_name`" is not defined in the ` "Admin" subclass.
In order to make things work I might create a subclass, but then I have to write something like
Code:
user1 = Admin("Arman", "Cam", "Male", 20, "Canada")
Which I do now want. I want ` "Admin" ` to be an attribute in `"User` Class".
So, how can I add `self.first_name` in `Admin` class without using subclass method?
Also since User have lots of arguments, is there a shortcut syntax for passing all arguments of `User` in other functions?
Note: By subclass method I mean that `Admin(User)`