Course Content
In-Depth Python OOP
In-Depth Python OOP
4. Polymorphism and Abstraction
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?
Everything was clear?
Thanks for your feedback!
SectionΒ 5. ChapterΒ 6