I would like to call the overridden abstract class method (subclass method) from the abstract class method, but getting several errors. Could you help me, please? My concept:
import abc
from my_module import Format, Message
class BaseClass(object):
"""
Object getting some messages and modifying them. Some modify functions differ
subclass to subclass.
"""
__metaclass__ = abc.ABCMeta
@classmethod
@abc.abstractmethod
def basic_format(cls, msg_):
"""Dynamically creates new format.
To be overridden in subclasses.
"""
raise NotImplementedError
message_formats_dict = {0x00: Format(basic_format)}
@classmethod
@abc.abstractmethod
def modify(cls, msg_type_id_, msg_, new_format_=None):
"""
Class method returning modified message according to given function.
:param int msg_type_id_: Message type.
:param msg_: Message to be changed.
:param new_format_: New format or a function defining
it dynamically
:returns: Modified message.
"""
if new_format_:
cls.message_formats_dict.update((msg_type_id_, new_format_))
return cls.message_formats_dict[msg_type_id_].produce_formatted_msg(msg_)
class B(BaseClass):
"""
Subclass of A.
"""
@classmethod
def basic_format(cls, msg_):
"""Overrides the BaseClass.basic_format, creates basic format for B
type.
"""
return Format(msg_[3], msg_[2], msg_[1])
@classmethod
def format_01(cls, msg_):
"""Creates format_01 for B type.
"""
return Format(msg_[2], msg_[1], msg_[3])
@classmethod
def modify(cls, msg_type_id_, msg_, new_format_=None):
"""Overrides the BaseClass.modify, adds new function.
"""
cls.message_formats_dict.update((0x01, format_01))
return super(B, cls).modify(cls, msg_type_id_, msg_, new_format_)
Well, I would like to call it both, on the class, and on an instance:
new_message_01 = B.modify(0x00, some_message)
new_message_02 = B().modify(0x00, some_message)
so, that it would use the subclass method B.basic_format overriding the BaseClass method.
Calling the format_01 method implemented in subclass B and referenced from B works fine:
new_message_03 = B.modify(0x01, some_message)
new_message_04 = B().modify(0x01, some_message)
I think the problem could in reference, the basic_format is referenced in BaseClass. But how by pass that?