Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Users Representation and Comparison | Magic Methods
In-Depth Python OOP
course content

Зміст курсу

In-Depth Python OOP

In-Depth Python OOP

1. OOP Concepts
2. Inheritance
3. Encapsulation
4. Polymorphism and Abstraction
5. Magic Methods

bookChallenge: 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 below and The new code is above will help you to find a new code for editing.
  • In addition, you need to add class attributes to the existing code.

Завдання

  1. Create a role class attribute with the value "User" inside the User class.
  2. Create a role class attribute with the value "Admin" inside the Admin class.
  3. Define the representation magic method inside the User class. This method should return the string "{role}: {username}".
    For example ("Admin: greatest.admin").
  4. Define the comparison magic method for the == operation. This method should compare:
    • if the taken data is a User instance - 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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 5
toggle bottom row

bookChallenge: 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 below and The new code is above will help you to find a new code for editing.
  • In addition, you need to add class attributes to the existing code.

Завдання

  1. Create a role class attribute with the value "User" inside the User class.
  2. Create a role class attribute with the value "Admin" inside the Admin class.
  3. Define the representation magic method inside the User class. This method should return the string "{role}: {username}".
    For example ("Admin: greatest.admin").
  4. Define the comparison magic method for the == operation. This method should compare:
    • if the taken data is a User instance - 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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 5
toggle bottom row

bookChallenge: 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 below and The new code is above will help you to find a new code for editing.
  • In addition, you need to add class attributes to the existing code.

Завдання

  1. Create a role class attribute with the value "User" inside the User class.
  2. Create a role class attribute with the value "Admin" inside the Admin class.
  3. Define the representation magic method inside the User class. This method should return the string "{role}: {username}".
    For example ("Admin: greatest.admin").
  4. Define the comparison magic method for the == operation. This method should compare:
    • if the taken data is a User instance - 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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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 below and The new code is above will help you to find a new code for editing.
  • In addition, you need to add class attributes to the existing code.

Завдання

  1. Create a role class attribute with the value "User" inside the User class.
  2. Create a role class attribute with the value "Admin" inside the Admin class.
  3. Define the representation magic method inside the User class. This method should return the string "{role}: {username}".
    For example ("Admin: greatest.admin").
  4. Define the comparison magic method for the == operation. This method should compare:
    • if the taken data is a User instance - 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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 5. Розділ 5
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt