'Creating classes dynamically in python [duplicate]
Is it a good coding practice to have the class name as variable. E.g
def create_class(class_name):
   class class_name:
     def __init__(self):
       do_sth....
   class_instance = class_name()
   return class_instance
for object in objects:
      res = create_class(object)
I wanted to create different classes which are present in a list like objects=[person,vehicle, location ..]. What could be other alternative way to do it ?
Solution 1:[1]
class_name is a class, you can set a name property for your class, like this:
class class_name:
    def __init__(self,name):
        self.name = name
    def __str__(self):
        return str(self.name)
objects=["person","vehicle","location"]
for obj in objects:
    res = class_name(obj)
    print(res)
    					Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | év3v | 
