gvwtia
Last Updated: November 16, 2017
·
13
· neutralino1
981494 10151466984546915 1613396205 o

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

Say Thanks
Respond