I'm a newbie python developer who accidentally studied the concept of Abstract classes and I found it pretty much helpful to implement due to the SOLID principles.
At the very moment of testing and learning the concept, I found out that using a abstract class also as an mix-in class would be a great Idea, so I decided to implement the __init__
method in the base abstract class.
As I am new to the concept, I don't know if this is a good implementation for making reusable software:
#!usr/bin/env python3
# -*- coding: utf-8 -*-
"""Dependency inversion learning file | By : KhodeNima |
2023/10/25
"""
from decimal import (
Decimal
)
from abc import (
ABC,
)
class BaseCurrency(ABC):
def __init__(self, value_by_dollar : Decimal):
"""The base currency class
Args:
- value_by_dollar (Decimal): The value of the currency in Dollar.
"""
self.__name = self.__class__.__name__
self.value_by_dollar = value_by_dollar
@property
def name(self):
return self._name
@name.setter
def name(self, name : str):
if name == "BaseCurrency":
raise PermissionError("The BaseCurrency class can only be inherited | ( Not instantiated )")
return name
class Dirham(BaseCurrency):
...
some_decimal_value = Decimal('10.0')
dirham_1 = Dirham(value_by_dollar=some_decimal_value)
Dirham
? It would be helpful for reviewers to see this implementation (as it clarifies how you intend the base to be extended). Question size limit is plenty large enough for you to fill that in - thanks! \$\endgroup\$dirham_1.name
results inAttributeError: 'Dirham' object has no attribute '_name'. Did you mean: 'name'?
Anddirham_1.name = "Name"
doesn't do anything. As such your code is just a long-winded way of storing onlyvalue_by_dollar
. \$\endgroup\$