Skip to content

Instantly share code, notes, and snippets.

@yen3
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yen3/f174f3576e7eac1bf25a to your computer and use it in GitHub Desktop.
Save yen3/f174f3576e7eac1bf25a to your computer and use it in GitHub Desktop.
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()
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