Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Summary | Magic Methods
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 the correct answer

question mark

What are magic methods?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 6

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 2.78

bookSummary

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 6
some-alt