metaclassing, defining __init__ to pass attributes for the instances (not for the class) using a dictionary

StackOverflow https://stackoverflow.com/questions/16918076

Вопрос

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top