Challenge: Users Representation and Comparison
There are many magic methods in Python, we have not considered all of them, but others are not used as often.
Let's go back to your code. Your User class hasn't representation, and you can't print user info using the print(user) construction. Also, there are moments when you need to check a username or user id with the user instance (account) that did actions in your application.
Note
The comments
The new code is belowandThe new code is abovewill help you to find a new code for editing.In addition, you need to add class attributes to the existing code.
Swipe to start coding
- Create a
roleclass attribute with the value"User"inside theUserclass. - Create a
roleclass attribute with the value"Admin"inside theAdminclass. - Define the representation magic method inside the
Userclass. This method should return the string"{role}: {username}".
For example ("Admin: greatest.admin"). - Define the comparison magic method for the
==operation. This method should compare:- if the taken data is a
Userinstance - compare the instance username and the taken instance username. - if the taken data is a string - compare the instance username and the taken string.
- if the taken data has another data type - method should return
False.
- if the taken data is a
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Zusammenfassen Sie dieses Kapitel
Code in file erklären
Erklären, warum file die Aufgabe nicht löst
Awesome!
Completion rate improved to 2.78
Challenge: Users Representation and Comparison
Swipe um das Menü anzuzeigen
There are many magic methods in Python, we have not considered all of them, but others are not used as often.
Let's go back to your code. Your User class hasn't representation, and you can't print user info using the print(user) construction. Also, there are moments when you need to check a username or user id with the user instance (account) that did actions in your application.
Note
The comments
The new code is belowandThe new code is abovewill help you to find a new code for editing.In addition, you need to add class attributes to the existing code.
Swipe to start coding
- Create a
roleclass attribute with the value"User"inside theUserclass. - Create a
roleclass attribute with the value"Admin"inside theAdminclass. - Define the representation magic method inside the
Userclass. This method should return the string"{role}: {username}".
For example ("Admin: greatest.admin"). - Define the comparison magic method for the
==operation. This method should compare:- if the taken data is a
Userinstance - compare the instance username and the taken instance username. - if the taken data is a string - compare the instance username and the taken string.
- if the taken data has another data type - method should return
False.
- if the taken data is a
Lösung
Danke für Ihr Feedback!
single