Last active
August 29, 2015 14:05
-
-
Save yen3/f174f3576e7eac1bf25a to your computer and use it in GitHub Desktop.
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
from __future__ import print_function | |
class Attribute(object): | |
def __init__(self): | |
pass | |
def main(): | |
a = Attribute(); | |
print(a.__dict__) | |
a.__setattr__("test", 5) | |
print(a.__dict__) | |
print(a.test) | |
if __name__ == '__main__': | |
main() |
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
from __future__ import print_function | |
class Attribute(object): | |
def __init__(self): | |
object.__setattr__(self, "attr", {"a": "123", "b": "456", "c": 5}) | |
def __setattr__(self, name, value): | |
if name in self.attr: | |
self.attr[name] = value | |
return | |
raise AttributeError | |
def __getattr__(self, name): | |
if name in self.attr: | |
return self.attr[name] | |
raise AttributeError | |
def __repr__(self): | |
return str(self.attr) | |
def __str__(self): | |
return self.__repr__() | |
def set_attributes(self, **args): | |
#for k, v in args.items(): | |
#print(k, v) | |
self.attr.update(args) | |
def main(): | |
x = Attribute() | |
x.a = 5 | |
x.c = 6 | |
x.set_attributes(d=7, aaa=15, eee=8) | |
print(x) | |
print(x.a) | |
print(x.b) | |
print(x.c) | |
print(x.d) | |
print(x.aaa) | |
print(x.eee) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment