Dynamic class creation

Even if I create the Example class dynamically, the results will be the same as in my previous post.

class Example:
    attr = 1

    def method(self):
        return "method"

name = "Example"
bases = ()
namespace = {
    "attr": 1,
    "method": lambda self: "method"
}

Example = type(name, bases, namespace)

print(f"{Example.__class__=}")     # <class 'type'>
print(f"{Example().attr=}")        # 1
print(f"{Example().method()=}")    # 'method'
assert isinstance(Example, type)
assert isinstance(Example(), Example)

Last update: June 2, 2023
Created: June 2, 2023