Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Summary | Magic Methods
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
In-Depth Python OOP

bookSummary

Свайпніть щоб показати меню

Now, look at the code that you have written and describe this for yourself:

from abc import ABC, abstractmethod


class AuthMixin:
    is_authenticated = False
    
    def login(self, taken_password):
        if self.password == taken_password:
            self.is_authenticated = True
            print(f"{self.username} is authenticated")
        else:
            print("Wrong password!")

    def logout(self):
        self.is_authenticated = False
        print(f"{self.username} is loggouted")


class AbstractAdmin(ABC):
    @abstractmethod
    def login():
        pass
    
    @abstractmethod
    def logout():
        pass
        
    @abstractmethod
    def create_content():
        pass
    
    @abstractmethod
    def update_content():
        pass
    
    @abstractmethod
    def delete_content():
        pass


class User(AuthMixin):
    role = "User"
    
    def __init__(self, username, password):
        self.username = username
        self.password = password
    
    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, new_password):
        if isinstance(new_password, str):
            if len(new_password) >= 8:
                self._password = new_password
            else:
                print("The password length should be >= 8.")
        else:
            print("The password should be string.")
            
    def __repr__(self):
        return f"{self.role}: {self.username}"
    
    def __eq__(self, other):
        if isinstance(other, User):
            return self.username == other.username
        
        if isinstance(other, str):
            return self.username == other
        
        return False


class Admin(User, AbstractAdmin):
    role = "Admin"
    
    def create_content(self):
        print(f"{self.username} creates the content")
        
    def update_content(self):
        print(f"{self.username} updates the content")
        
    def delete_content(self):
        print(f"{self.username} deletes the content")

You can understand this code with 85 lines. Congratulations!
You have mastered Object-Oriented Programming in Python and can use the necessary functionality!

1. Remember all OOP concepts:

2. What are magic methods?

question mark

Remember all OOP concepts:

Select all correct answers

question mark

What are magic methods?

Select the correct answer

Все було зрозуміло?

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

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

Секція 5. Розділ 6

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 5. Розділ 6
some-alt