一、反射

   通过字符串的形式操作对象相关的属性。(使用字符串数据类型的变量名来获取这个变量的值)

    Python中的一切事物都是对象(都可以使用反射)

      反射类中的变量

      反射对象中的变量

      反射模板中的变量

      反射本文件中的变量

    用反射的场景:

      input

      网络

      文件

#hasattr

def hasattr(*args, **kwargs): # real signature unknown
    """
    Return whether the object has an attribute with the given name.
    
    This is done by calling getattr(obj, name) and catching AttributeError.
    """
    pass



#getattr

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

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

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
反射的4种方法源码

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-10
  • 2022-12-23
  • 2022-12-23
  • 2018-09-13
  • 2021-12-01
猜你喜欢
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
  • 2021-11-06
  • 2021-09-09
  • 2021-11-09
相关资源
相似解决方案