test_\@.py
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
def args(*args, **kwargs):
def _decorator(func):
print "befor ", func.__dict__
func.__dict__.setdefault(‘options‘, []).insert(0, (args, kwargs))
print ‘end ‘, func.__dict__
return func
return _decorator
@args(‘-p‘, ‘--pp‘, type=‘int‘)
@args(‘-e‘, ‘--ee‘, type=‘int‘)
def set(pp, ee):
print ‘---------‘, pp, ee
def test():
set(11, 22)
print "\ndir(set) ------"
print dir(set)
print "\nset.__dict__ -------"
print set.__dict__
print "\ngetattr(set, ‘options‘) --------"
print getattr(set, ‘options‘)
if __name__ == "__main__":
test()
执行结果
befor {}
end {‘options‘: [((‘-e‘, ‘--ee‘), {‘type‘: ‘int‘})]}
befor {‘options‘: [((‘-e‘, ‘--ee‘), {‘type‘: ‘int‘})]}
end {‘options‘: [((‘-p‘, ‘--pp‘), {‘type‘: ‘int‘}), ((‘-e‘, ‘--ee‘), {‘type‘: ‘int‘})]}
--------- 11 22
dir(set) ------
[‘__call__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__delattr__‘, ‘__dict__‘, ‘__doc__‘, ‘__format__‘, ‘__get__‘, ‘__getattribute__‘, ‘__globals__‘, ‘__hash__‘, ‘__init__‘, ‘__module__‘, ‘__name__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘func_closure‘, ‘func_code‘, ‘func_defaults‘, ‘func_dict‘, ‘func_doc‘, ‘func_globals‘, ‘func_name‘, ‘options‘]
set.__dict__ -------
{‘options‘: [((‘-p‘, ‘--pp‘), {‘type‘: ‘int‘}), ((‘-e‘, ‘--ee‘), {‘type‘: ‘int‘})]}
getattr(set, ‘options‘) --------
[((‘-p‘, ‘--pp‘), {‘type‘: ‘int‘}), ((‘-e‘, ‘--ee‘), {‘type‘: ‘int‘})]
dict.py
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
class Person:
def __init__(self,_obj):
self.__dict__.update(_obj)
if __name__ == "__main__":
person = Person({‘name‘:"n", "age":‘3‘})
print person.name
print person.age
print person.__dict__
print Person.__dict__
执行结果
n
3
{‘age‘: ‘3‘, ‘name‘: ‘n‘}
{‘__module__‘: ‘__main__‘, ‘__doc__‘: None, ‘__init__‘: <function __init__ at 0x7f606e65cb90>}