Created
March 6, 2011 16:06
-
-
Save yosisa/857379 to your computer and use it in GitHub Desktop.
This file contains 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 Foo(object): | |
def __init__(self, *names): | |
self._d = {} | |
for name in names: | |
self.add_property(name, name) | |
def add_property(self, name, default): | |
self._d[name] = default | |
if not hasattr(Foo, name): | |
setattr(Foo, name, self._add_property(name)) | |
def _add_property(self, name): | |
def getter(self): | |
return "[[ {0} ]]".format(self._d.get(name, None)) | |
def setter(self, value): | |
self._d[name] = str(value) | |
return property(getter, setter) | |
foo = Foo("foo", "bar", "baz") | |
print foo.foo | |
print foo.bar | |
print foo.baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment