Ruby-like OpenStruct in Python
In Ruby, Struct
s and OpenStruct
s 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
Written by Emmanuel Turlay
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#