zoukankan      html  css  js  c++  java
  • python __dict__

    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 " dir(set) ------"
    print dir(set)
    print " set.__dict__ -------"
    print set.__dict__
    print " getattr(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>}

  • 相关阅读:
    打sql server pack4后打开网站报错的解决办法
    北京大学的三角形文章
    一次SQL Server 2000修复实践的说明
    今天重看了几集《将爱情进行到底》
    MakeFile的写法
    [经验杂谈]与大虾对话:领悟设计模式zz
    论函数调用约定(zz)
    用标准模板库STL实现文件比较(zz)
    C++中的虚函数(virtual function)
    为学院科研办做的个小应用管理程序
  • 原文地址:https://www.cnblogs.com/banwhui/p/4877731.html
Copyright © 2011-2022 走看看