Dynamic class with inheritance in python

How can I dynamically create classes in a class? As I tried below, I get a NameError: the name 'Foo is not defined. I am completely new to Python, please forgive me if this is obvious.

class Parent(object):
    name2class = {'foo' : Foo, 'bar' : Bar }

    def do(self,string):
        return name2class[string]()

class Foo(Parent):
    pass

class Bar(Parent):
    pass

if __name__ == '__main__':
    parent = Parent()
    instance = parent.do()
+4
source share
4 answers

As you wrote this, you need to define Fooand Barabove Parent, as it Parentrefers to other classes.

Edit: you just need to move these class references to a method:

class Parent(object):
    def do(self,string):
        name2class = {'foo' : Foo, 'bar' : Bar }
        return name2class[string]()

class Foo(Parent):
    pass

class Bar(Parent):
    pass

if __name__ == '__main__':
    parent = Parent()
    instance = parent.do('foo')

Edit2: Here is your factory version:

class Parent(object):
    # Add shared methods here
    pass

class Foo(Parent):
    # Add unique methods
    pass

class Bar(Parent):
    # Add unique methods
    pass

class ParentFactory(object):
    def __init__(self):
        self.name2class = {'foo' : Foo, 'bar' : Bar}

    def create(self, string):
        return self.name2class[string]()

if __name__ == '__main__':
    factory = ParentFactory()
    instance = factory.create('foo')
+3
source

The Foo and Bar classes were not defined by instantly creating name2class names. Another error was not passed to the Parent.do () parameter

class Parent(object):
    def __init__(self):
        self.name2class = {'foo' : Foo, 'bar' : Bar}

    def do(self, string):
        return self.name2class[string]()

class Foo(Parent):
    pass

class Bar(Parent):
    pass

if __name__ == '__main__':
    parent = Parent()
    instance = parent.do('foo')
+1

python type(name, bases, dict) functuion:...


OK. :

class Parent(object):

        childs = {}
        def somefunc(self):
                print "Hello from, %s"%self

        def do(self, string):

                return self.childs[string]()

class Foo(Parent):

        pass

class Bar(Parent):

        pass

parent = Parent()
parent.somefunc()
parent.childs["foo"] = Foo
parent.childs["bar"] = Bar
foo = parent.do("foo")
foo.somefunc()
bar = parent.do("bar")
bar.somefunc()

:

Hello from, <__main__.Parent object at 0x...>
Hello from, <__main__.Foo object at 0x...>
Hello from, <__main__.Bar object at 0x...>
+1

The definition of a new child class should not affect the implementation of the base class. The corresponding design pattern is Factory. There are many ways to implement it, from a simple function to a dynamic registration mechanism. The simplest version:

class Parent(object):
   # your code here
   pass

class Child1(Parent):
   # XXX

class Child2(Parent):
   # XXX

class Child2(Parent):
   # XXX


CLSMAP = {
    "name1": Child1,
    "name2": Child2,    
    "name3": Child3,
    }   

def create(name, *args, **kw):
   return CLSMAP[name](*args, **kw)
0
source

All Articles