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

Course Content

In-Depth Python OOP

In-Depth Python OOP

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

book
Summary

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 the correct answer

question mark

What are magic methods?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

In-Depth Python OOP

In-Depth Python OOP

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

book
Summary

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 the correct answer

question mark

What are magic methods?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 6
some-alt