Created
March 6, 2011 14:02
-
-
Save yosisa/857306 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): | |
_d = {} | |
def __init__(self, *names): | |
for name in names: | |
self.add_property(name, name) | |
def add_property(self, name, value): | |
self._d[name] = value | |
def getter(self, value): | |
return "[[ {0} ]]".format(value) | |
def setter(self, value): | |
return value | |
def __getattr__(self, name): | |
if name not in self._d: | |
raise AttributeError | |
return self.getter(self._d[name]) | |
def __setattr__(self, name, value): | |
if name in self._d: | |
self._d[name] = self.setter(value) | |
else: | |
object.__setattr__(self, name, value) | |
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