More about Alcides Fonseca
Go Back

Python Geekness

Written by Alcides Fonseca at 2008/02/07

After I read that OpenStruct was now in Ruby core, I had to do it in Python:



class OpenStruct:
def __init__(self, **dic):
self.__dict__.update(dic)
def __getattr__(self, i):
if i in self.__dict__:
return self.__dict__[i]
else:
raise AttributeError, i
def __setattr__(self,i,v):
if i in self.__dict__:
self.__dict__[i] = v
else:
self.__dict__.update({i:v})

o = OpenStruct(b=2)
o.a=1
print o.a,o.b


Will they take it into the Python core? Nooo, but is cool to code nevertheless, and may be used sometimes :)