Last Updated: November 16, 2017
·
3.127K
· neutralino1

Ruby-like OpenStruct in Python

In Ruby, Structs and OpenStructs are really practical to hold various config data types. Here is a similar tool for Python:

from collections import namedtuple

def Struct(**kwargs):
    return namedtuple('Struct', ' '.join(kwargs.keys()))(**kwargs)

Which can be used as such:

>>> settings = Struct(settings1=1, settings2=2)
>>> settings.settings1
1

Cheers