merano
merano

Reputation: 15

how to create dynamic class name using dictionary?

Is this possible to create dynamic class name using dictionary in python

a={'a':'hai','b','hello'}

I expected class name as

class hai():
  class content
class hello():
  class content 

Upvotes: 1

Views: 80

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250931

Usually it's not a good idea to create global variables using globals(), but here you go using globals() and 3 argument version of type().

>>> a = {'a':'hai','b': 'hello'}
>>> for v in a.values():
    globals()[v] = type(v, (), {})


>>> hai
<class '__main__.hai'>
>>> hello
<class '__main__.hello'>

Upvotes: 4

Related Questions