Reputation: 43
ok I'm trying to make a code to display a list of admin privileges. I made the code and things work but it gives me a weird display before any of the list items:
input code:
class Privileges:
def __init__(*privileges):
privileges
def show_privileges(*privileges):
print("these are your privileges:")
for privilege in privileges:
print(f"\t{privilege}")
class Admin(User):
def __init__(self, first_name, last_name, age, username):
super().__init__(first_name, last_name, age, username)
self.privileges = Privileges()
Admin = Admin('Admin', '', '' ,'')
Admin.privileges.show_privileges('can add post', 'can delete post',
'can ban user')
output:
these are your privileges:
<__main__.Privileges object at 0x7facd3c5f4f0>
can add post
can delete post
can ban user
Upvotes: 1
Views: 79
Reputation: 2236
The function show_privileges(*privileges)
is being given self
as the first argument. That is the weird output that you are seeing, self is being printed. You need to either include self in the definition as:
def show_privileges(self, *privileges):
print("these are your privileges:")
for privilege in privileges:
print(f"\t{privilege}")
Or you can slice the list to avoid the first element:
def show_privileges(*privileges):
print("these are your privileges:")
for privilege in privileges[1:]:
print(f"\t{privilege}")
The first option would be more typical i think.
To read more about self
and how it works in python you can read the tutorial mentioned by codewelldev.
Upvotes: 2