Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate many objects into one object using inheritance in python? (during runtime)

I have the following classes:

class hello(object):
    def __init__(self):
        pass

class bye(object):
    def __init__(self):
        pass

l = [hello, bye]

If I do the following I get an error:

>>> class bigclass(*l):
  File "<stdin>", line 1
    class bigclass(*l):
                    ^
SyntaxError: invalid syntax

Is there another way to do this automatically at runtime?

I am using Python 2.7.

like image 859
Har Avatar asked Dec 06 '13 14:12

Har


1 Answers

You could use the 3-argument form of type to create the class:

bigclass = type('bigclass', (hello, bye), {})
like image 68
unutbu Avatar answered Sep 22 '22 08:09

unutbu