I have a class "A" below, that have a very simple
__init__ method, which only wants a "needle".


class A:
    def __init__(self, needle):
        self.needle = needle

    @classmethod
    def from_haystack(cls, haystack):
        # ...
        # Many lines of code (omitted here)
        # that process haystack to obtain a needle
        # ...
        # ...
        # The obtained needle is then used to
        # return an instance
        return cls(needle)


The class also has a classmethod: "from_haystack"
that is used as an alternative to __init__ for
initalizing an instance.

My problem is that:
  1. I'm not allowed to refactor A.
  2. I need a class B which must inherit A.
  3. B must use a haystack to initialize
     itself.
  4. Therefore, I want to reuse "from_haystack" from A
     in some way so I don't have to repeat the code.

My naive solution was this:


class B(A):
    def __init__(self, haystack, spam, eggs):
        super().from_haystack(haystack)  # Won't work
        self.spam = spam
        self.eggs = eggs


The use of super() here will call "from_haystack" with
cls = B, which won't work, since the return line will be
"return B(needle)" and B can't handle that.

What is idiomatic solution to this problem?
Thanks,

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to