• 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>}

  • 相关阅读:
    VS2008找不到MFC90d.dll错误解决方法
    字符编码之间的转换
    java 中使用RSA非对称性加密解密
    java eclipse中使用wsdl生成soap 的客户端代码
    java 打印空心菱形的两种实现
    Chrome 快捷键
    电脑常用快捷键
    VS2013常用快捷键
    Eclipse常用快捷键
    java 使用for循环打印杨辉三角形
  • 原文地址:https://www.cnblogs.com/banwhui/p/4877731.html
Copyright © 2020-2023  润新知