calling subclass constructor question

I

In Han Kang

So each of the sub classes plots a different type of graph. The
superclass defines methods that are the same for all the plots. I want
to be able to pick some points and be able to generate a more plots.
What I was wondering if I could define in a method in the superclass of
an object the ability to make a brand new subclass (new plot). So
basically, one plot makes another plot, but it'd be nice if i could put
all the code in the superclass.

Thank you again for all your thoughts and helps.
 
D

D H

In said:
So each of the sub classes plots a different type of graph. The
superclass defines methods that are the same for all the plots. I want
to be able to pick some points and be able to generate a more plots.
What I was wondering if I could define in a method in the superclass of
an object the ability to make a brand new subclass (new plot). So
basically, one plot makes another plot, but it'd be nice if i could put
all the code in the superclass.

Right, I agree with Steven, that you are probably wanting something like
the factory pattern. Here's something modified from Steven's example
more like what you are asking for:

class Plot(object): #you need to subclass object to get __subclasses__

@staticmethod
def getplot(name):
return [plot for plot in
Plot.__subclasses__() if plot.__name__ == name][0]()


class LinePlot(Plot):
pass

class BarGraph(Plot):
pass

lp = Plot.getplot("LinePlot")

bar = Plot.getplot("BarGraph")

See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/86900
 
S

Steven Bethard

In said:
So each of the sub classes plots a different type of graph. The
superclass defines methods that are the same for all the plots. I want
to be able to pick some points and be able to generate a more plots.
What I was wondering if I could define in a method in the superclass of
an object the ability to make a brand new subclass (new plot). So
basically, one plot makes another plot, but it'd be nice if i could put
all the code in the superclass.

Still not sure I understand you. How are you determining which new type
of plot to create? If I'm an instance of class PlotA, and the user asks
me to create create a new Plot, is it always another instance of class
PlotA, or could it be an instance of another class?

If the former, you can use a classmethod, e.g.:

class Plot(object):
@classmethod
def new(cls, ...):
result = cls(...)
# maybe modify result some more
return result

If the latter, this doesn't sound like something that should be a method
of an instance. It sounds more like a factory function. Why not just
make it a module-global function, e.g.:

def newPlot(...):
# determine which kind of Plot object to create
...
if ...:
return PlotA(...)
...

A method signature of the method you want to write would help a lot...

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,252
Messages
2,571,267
Members
47,908
Latest member
MagdalenaR

Latest Threads

Top