1 Upvote

Dynamically Create Classes with Python

Python
General Python

class DynamicClass():
    def __init__(self):
        value = 0

num_classes = 5
dynamic_class = [DynamicClass() for i in range(num_classes)]  

for i in range(num_classes):
    dynamic_class[i].class_number = i

print(dynamic_class[2].class_number)

By detro - Last Updated April 7, 2022, 10:44 a.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Python For Else Loop
Python
General Python

Loop

2
Top Contributors
103
100