[Tutor] Creating instance of child classes dynamically

Sachit Murarka connectsachit at gmail.com
Sun Feb 7 10:18:22 EST 2021


I meant base class in a different python file. It was a typo.

Example :

parent.py
class Parent():
    @abstractmethod
    def process(self):

child1.py
class Child1():
      def process(self):
          #Doing something

child2.py
class Child2():
      def process(self):
          #Doing something else


My use case is , Parent will define the template. On the run type user will
pass a string , eg String will be child1 or child2 etc. Accordingly we want
to call process function of that class.

Please note each child class is in a different file . In actual solution
there will be many children and we can not put all children in a single py
file.

As using reflections we can create objects at run time by giving fully
qualified class names(Atleast in java). I have worked in Java not in Python
much, so trying to achieve something similar.  In java we used to do this:

String input="Child1";
Parent p = Class.forName(input);
p.process() // this will call the method of base class which was stored in
the input variable.




Kind Regards,
Sachit Murarka


On Sun, Feb 7, 2021 at 8:29 PM Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 07/02/2021 13:22, Sachit Murarka wrote:
>
> > I have different classes in different python files. There are child
> classes
> > and I do have a base class in a different class.
>
> I'm not sure what that last bit means? "A base class in a different
> class" Do you mean you have created the base class inside some other
> class? That's extremely unusual, is there a reason? Or do you just mean
> you have a seperate base class?
>
> > My input to the python will be a string , according to the string I have
> to
> > create an object of child class and call the functions.
>
> Presumably you mean call the methods, not functions?
> But otherwise that's fairly straightforward, you can
> test your string and create whichever type of object
> is required. Presumably there is something in the
> string that lets you decide which class you need?
>
> > Here I have to use the concept of reflection + dynamic polymorphism.
>
> In python all polymorphism is dynamic.
> And I'm not sure why you would need reflection.
> I don't see anything in your problem that looks
> like a candidate.
>
> > Could anyone please help me here?
>
> You probably need to be more specific.
> May be give is two input samples and tell us which class
> you need for each, and how you decided that. Once
> see see a few examples we can help find a
> programmable solution.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list