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

  • 相关阅读:
    Python_soket
    Python_正则表达式语法
    Python_math模块
    Python_random模块
    Python_os模块
    Python_time模块
    Java技能树-图片版
    读书笔记---《编写可读代码的艺术》
    Java代码优化建议
    Git常用命令
  • 原文地址:https://www.cnblogs.com/banwhui/p/4877731.html
Copyright © 2011-2022 走看看