0

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?

Petr Krampl
  • 816
  • 9
  • 11
  • 2
    Please post a minimal self contained example and the "several errors" you get. – bruno desthuilliers Jan 14 '15 at 12:16
  • 1
    What is `basic_format`? – unutbu Jan 14 '15 at 12:28
  • Please also remove all the excessive whitespace in the code you post here, it's difficult enough to read here on my desktop it's so long (and I hate to think what it's like for any poor soul trying to read it on their tablets or smartphones.) – martineau Jan 14 '15 at 12:30
  • What is `format_01`? – martineau Jan 14 '15 at 12:42
  • @martineau: hrm, smartphones with the app make it super easy to read that code. Are you saying the nice PEP-8 whitespace in the question is now a bad thing? The OP should instead cut it down to a MVE, the whitespace is not the problem here. – Martijn Pieters Jan 14 '15 at 12:58
  • Thanks for the comments. I have modified the question the methods' names to be consistent: @unutbu: basic_format was typo of basic_modification method. – Petr Krampl Jan 14 '15 at 13:35
  • @martineau: The code is formatted due to PEP-8 (mostly I hope ;-) ). The editor made the whitespaces larger sometimes. I am sorry, you have problem to read it on a small screen. format_01 is a classmethod of subclass B included in cls.message_formats_dict callable with B.modify or B().modify – Petr Krampl Jan 14 '15 at 13:36
  • @MartijnPieters: Here on my 24" monitor I can only see slightly less than half of the code fragment at once -- and a fair about of it is just vertical whitespace. Maybe SO should develop a browser extension to their website, too. Following the PEP-8 style guidelines in this regard in code snippets in questions posted here on SO might be detrimental. – martineau Jan 14 '15 at 14:09
  • @PetrKrampl: It's just one of the undefined things I ran into trying to get your code running so I could see the problem(s) you're talking about. I think the most helpful thing you could do is come up with a [`SSCCE`](http://sscce.org/) (Short, Self Contained, Correct (Compilable), Example) illustrating the issues you're encountering. – martineau Jan 14 '15 at 14:20

0 Answers0