Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 297|回复: 0

[默认分类] Python的getattr(),setattr(),delattr(),hasattr()

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-24 17:59:47 | 显示全部楼层 |阅读模式
    getattr()函数是python自省的核心函数,具体使用大体如下:
    获取对象引用getattr
    Getattr用于返回一个对象属性,或者方法


      
      
       Python代码
       
      
      
      class A:     
          def __init__(self):     
              self.name = "zhangjing"   
          #self.age="24"
          def method(self):     
              print"method print"   
          
      Instance = A()     
      print getattr(Instance , "name, "not find") #如果Instance 对象中有属性name则打印self.name的值,否则打印"not find"
      print getattr(Instance , "age", "not find")   #如果Instance 对象中有属性age则打印self.age的值,否则打印"not find"
      print getattr(a, "method", "default")     
      #如果有方法method,否则打印其地址,否则打印default     
      print getattr(a, "method", "default")()     
      #如果有方法method,运行函数并打印None否则打印default   



    注:使用getattr可以轻松实现工厂模式。
    例:一个模块支持HTML、text、xml等格式的打印,根据传入的formate参数的不同,调用不同的函数实现几种格式的输出
      

      
      
       Python代码
       
      

       
       import statsout   
       def output(data, format="text"):                                 
            output_function = getattr(statsout, "output_%s" % format)   
           return output_function(data)   
      
      
       
      
       
       
        setattr(
        object, name, value)
       
       
      
    This is the counterpart of getattr(). The arguments
    are an object, a string and an arbitrary value. The string may name an existing
    attribute or a new attribute. The function assigns the value to the attribute,
    provided the object allows it. For example,
    1. setattr(x,
    2. "foobar", 123)
    复制代码
    is equivalent to
    1. x.foobar = 123
    复制代码
    .
      这是相对应的getattr()。参数是一个对象,一个字符串和一个任意值。字符串可能会列出一个现有的属性或一个新的属性。这个函数将值赋给属性的。该对象允许它提供。例如,setattr(x,“foobar”,123)相当于x.foobar = 123。
      
       
       
        delattr(
          
          
          
        object, name)
       
       
      
    This is a relative of setattr(). The arguments are
    an object and a string. The string must be the name of one of the object’s
    attributes. The function deletes the named attribute, provided the object allows
    it. For example,
    1. delattr(x, "foobar")
    复制代码
    is
    equivalent to
    1. del x.foobar
    复制代码
    .
    与setattr()相关的一组函数。参数是由一个对象(记住python中一切皆是对象)和一个字符串组成的。string参数必须是对象属性名之一。该函数删除该obj的一个由string指定的属性。
    1. delattr(x, "foobar")=
    复制代码
    1. del x.foobar
    复制代码

       
       

       hasattr用于确定一个对象是否具有某个属性。
    1. 语法:
    2. hasattr(object, name) -> bool
    3. 判断object中是否有name属性,返回一个布尔值。
    复制代码

      
    >>> li=["zhangjing","zhangwei"]
    >>> getattr(li,"pop")
    <built-in method pop of list object at 0x011DF6C0>
    >>> li.pop
    <built-in method pop of list object at 0x011DF6C0>
    >>> li.pop()
    "zhangwei"
    >>> getattr(li,"pop")()
    "zhangjing"
    >>>getattr(li, "append")("Moe")
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-25 07:31 , Processed in 0.377837 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表