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 )
No problem. You just need to write the function and include it in the dictionary that you pass to type
under the key '__init__'
def func(self,*args,**kwargs):
self.args = args
for k,v in kwargs.items():
setattr(self,k,v)
MyClass = type('Employee',(object,),{'__init__':func})
e = MyClass(name='john')
print (e.name) #'john'
You can even "delete" func
when you're done creating the class if it makes you feel better about keeping your namespace clear:
MyClass = type('Employee',(object,),{'__init__':func})
#clean up, clean up, everybody everywhere, clean up clean up, everybody do your share ...
del func
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.