0

I was building several classes, and those class and its method need to be passed by method from another class.

class SimplePlotGenerator:

    def __init__(self):
        self.phrase = 'Something happens'
    def generate(self):
        return self.phrase

and here another class should do.

pv = PlotViewer()
pv.register(SimplePlotGenerator())

pv.generate()
'Something happens'

I have two more class for the PlotGenerator.

My question is how to build that register method. I think it might be some sort of multiple inheritances but I kind of stuck there.

1 Answer 1

1

It's not 100% clear but I think you probably just want to make this a constructor parameter:

class PlotViewer:
    def __init__(self, generator):
        self.generator = generator

    def generate(self):
        self.generator.generate()

# To use
pv = PlotViewer(SimplePlotGenerator())
pv.generate()

I'm assuming you want a parameter rather than inheritance as the names "generator" and "viewer" sort of imply that it is not true that a SimplePlotGenerator "is a" PlotViewer. However, if a plot generator is a kind of plot viewer then yes, you might want to have SimplePlotGenerator inherit from SimplePlotViewer.

Sign up to request clarification or add additional context in comments.

6 Comments

I think you might be right about the parameter, I'm kind of confused. The other function will have the same generate() but different output. Is there anyway we can build the register() to take any classes? I mean not just my pre-build class. Other class has input (.txt), I want it to be flexible which I can accept file type too.
There isn't any restriction on the type of class in the example I sent; as long as the class has a generate() method it'll work. In my example it's a constructor parameter but you can easily adapt my example to have no constructor argument and instead it has a def register(self, generator) method which just calls self.generator = generator. That would work the same way and it also wouldn't have any restrictions on which classes it could take - as long as the class has a generate method.
Got it. Also, when I call the register(arg). It needs to be stored with variable too right? I tried without storing and It showed an error '''AttributeError: 'PlotViewer' object has no attribute 'generate'
Correct, you need to store it in a variable on the self object.
Have some extra question one Inheritance I used on generator classes. I have built those class through inherit. Like 3 inherit from 2 and 2 inherit from 1. My question is, Is there a way that it will work with PlotViewer. I did try, there was no error but the result is not right. So, I went back on those classes and set it to be more independent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.