zoukankan      html  css  js  c++  java
  • Python—反射

     反射

    1 什么是反射

    反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。

    2 python面向对象中的反射:通过字符串的形式操作对象相关的属性。python中的一切事物都是对象(都可以使用反射)

    四个可以实现自省的函数 下列方法适用于类和对象(一切皆对象,类本身也是一个对象)

    hasattr(object,name)

    判断object中有没有一个name字符串对应的方法或属性
    

    getattr(object, name, default=None)

    def getattr(object, name, default=None): # known special case of getattr
        """
        getattr(object, name[, default]) -> value
    
        Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
        When a default argument is given, it is returned when the attribute doesn't
        exist; without it, an exception is raised in that case.
        """
        pass
    

    setattr(x, y, v)

    def setattr(x, y, v): # real signature unknown; restored from __doc__
        """
        Sets the named attribute on the given object to the specified value.
    
        setattr(x, 'y', v) is equivalent to ``x.y = v''
        """
        pass
    

    delattr(x, y)

    def delattr(x, y): # real signature unknown; restored from __doc__
        """
        Deletes the named attribute from the given object.
    
        delattr(x, 'y') is equivalent to ``del x.y''
        """
        pass
  • 相关阅读:
    Programming Collecive Intelligence 笔记 Making Recommendations
    Managing Gigabytes文本压缩
    Hadoop The Definitive Guide 笔记二
    POS Tagging with NLTK
    MG查询
    MG索引构造
    对SharePoint 2010的job failover的一些比较深入的说明
    SharePoint 2010中Search功能的数据库连接字符串在哪里?
    记解决一个数据库删不掉的问题
    SharePoint升级失败?
  • 原文地址:https://www.cnblogs.com/zivli/p/9906405.html
Copyright © 2011-2022 走看看