Dynamic property creation for a Python class

Dynamic property creation for a Python class

My collection of Python snippets

ยท

1 min read

I found this on (hmmm certainly) StackOverflow ๐Ÿ˜Š

# Dynamic property for a class
class ExampleClass():
    def __init__(self, argv):
        for key, val in argv.items():
            self.__dict__[key] = val

if __name__ == '__main__':
    argv = {'intro': 'Hello World!'}
    instance = ExampleClass(argv)
    print instance.intro

Updates 2023/06: using __setattr__()

https://www.pythonmorsels.com/python-setattr/
ย