Similarly to this question I would like to pass the attributes from a dictionary to an instance of a class, but creating the class using type
, like this:
attrs = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }
e = type('Employee', (object,),attrs)()
But when I do this the attributes are belonging to the class, not only to the instance. If I create two instances using:
e = type('Employee', (object,), attrs)()
f = type('Employee', (object,), attrs)()
They will actually be two different classes.
I wanted to create a class that accepts **kwargs
and *args
in __init__()
, like:
class Employee(object):
def __init__(self, *args, **kwargs):
self.args = args
for k,v in kwargs.items():
setattr(self, k, v)
using type()
. So that:
MyClass = type('Employee', (object,), {})
Would allow:
e = MyClass( **kwargs )