Created
May 23, 2020 18:20
-
-
Save suzukimilanpaak/ad529ae4b37e0313e573ef3f47f289a4 to your computer and use it in GitHub Desktop.
Ruby's Openstruct alternative in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OpenStruct: | |
def __init__(self, **dic): | |
self.__dict__.update(dic) | |
def __getattr__(self, i): | |
if i in self.__dict__: | |
return self.__dict__[i] | |
else: | |
None | |
def __setattr__(self,i,v): | |
if i in self.__dict__: | |
self.__dict__[i] = v | |
else: | |
self.__dict__.update({i:v}) | |
return v # i like cascates :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment