I'm creating a function that creates a class inside it and returns that...
def create_object(**objects):
# creates class, NewClass
class NewClass:
pass
for key in objects.keys():
# creating objects in NewClass
exec("NewClass.%s = %s" % (key, objects[key]))
return NewClass
and when we called this function,
new_object = create_object(first_name='Mayank', last_name='Tyagi')
new_object.first_name
this works fine and gives output
>>> 'Mayank'
but I want to create a function that will create a class with variable name. eg
def object(class_name, **objects):
# create a class where the value of class_name is the name of class
exec("class %s:\n\tpass" % (class_name))
for key in objects.keys():
# creating new objects in this class
exec("NewClass.%s = %s" % (key, objects[key]))
and after calling the function,
object('ClassName', first_name='Mayank', last_name='Tyagi')
ClassName.first_name
it should give the output,
>>>'Mayank'
How to make this?
-asking for help with a hope:)