I want to dynamically create subclasses from a superclass using strings as the subclasses names. For example, say I have this class representing an HTML tag.
class Tag:
def __init__(self, content, **attr):
pass
def __repr__(self):
return f"<{self.__class__.__name__.lower()}>{content}</{<self.__class__.__name__.lower()>}>"
And I also have this list:
class_names = ["Div", "A", "Body", "Html", "Nav"] # etc. Basically contains all html tags.
I want to somehow create subclasses (maybe using a for loop) of the Tag class, i.e. I want to have new classes namely Div, A, Body, Html, Nav, etc.
that are all subclasses of the Tag class. Is there a quick way to do this? Also is it bad practice to not explicitly declare a class?
Edit: I want to create subclasses, not objects.
Edit 2: As Goyo stated that my purpose is not clear, I want to basically implement all html tag classes in one go. Many people have already implemented HTML generators, but as for this example, they had to define all subclasses of the Tag class and just write pass
in the subclass, like this:
...
class Address(HtmlTag):
"""Defines contact information for the author/owner of a document"""
pass
class Applet(HtmlTag):
"""Not supported in HTML5. Use <embed> or <object> instead."""
pass
class Area(SelfClosingHtmlTag):
"""Defines an area inside an image-map"""
pass
...
Which is what I want to avoid, since this looks like redundant duplicated code and a lot of copy-pasting to me.
Edit 3: My apologies for mentioning other people's code as a "bad" example. That's not my intention in any way. I just want to learn how to write shorter and more concise code.