网站开发协同,mp6 wordpress 静态,网站制作类软件推荐,长沙网站推广排名优化DAY 5. python自省
这是很久之前写的#xff0c;当时对自省和反射的概念没理解#xff0c;学习Java以后多了一点理解#xff0c;自省是获取对象的能力#xff0c;反射是操纵对象的能力#xff0c;python中使用getattr()和setattr()实现反射#xff0c;而其他的则是自省当时对自省和反射的概念没理解学习Java以后多了一点理解自省是获取对象的能力反射是操纵对象的能力python中使用getattr()和setattr()实现反射而其他的则是自省下面的内容是把两者混在一起说的但又不想改了内容罗里吧嗦把最终的总结提到前面
方法作用help()查看函数或模块用途的详细说明dir()返回对象所有属性type()查看对象类型hasattr()查看对象是否有特定属性getattr()得到对象的特定属性setattr()设置对象的特定属性isinstance()判断一个对象是否是一个已知的类型issubclass()判断一个类是不是另一个类的子类id()返回地址值callable()判断对象是否可调用In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability. 在计算机科学中内省是指计算机程序在运行时Run time检查对象Object类型的一种能力通常也可以称作运行时类型检查 这是维基百科对自省内省的解释,通俗来说自省就是在程序运行过程中能够知道对象的类型的一种能力大部分语言都有这种能力都有办法在运行过程中知道对象的类型如cJava等
当然自省不仅仅只针对对象的类型如python自省还能知道对象的属性还有一些其他的理解 在日常生活中自省introspection是一种自我检查行为。 在计算机编程中自省是指这种能力检查某些事物以确定它是什么、它知道什么以及它能做什么。自省向程序员提供了极大的灵活性和控制力。 说的更简单直白一点自省就是面向对象的语言所写的程序在运行时能够知道对象的类型。简单一句就是运行时能够获知对象的类型。 例如c自省(来自维基百科)
C 通过运行时类型信息RTTItypeid和dynamic_cast关键字支持类型内省。 dynamic_cast表达式可用于确定特定对象是否属于特定派生类。 例如
Person* p dynamic_castPerson *(obj);
if (p ! nullptr) {p-walk();
}typeid运算符检索std :: type_info对象该对象描述对象的派生类型
if (typeid(Person) typeid(*obj)) {serialize_person( obj );
}
php自省(来自维基百科)
在php中可以使用instanceof运算符判断一个PHP变量是否属于某一类的实例
if ($obj instanceof Person) {// Do whatever you want
}Java自省(来自维基百科)
Java中类型自省的最简单示例是instanceof运算符。 instanceof运算符确定特定对象是属于特定类或该类的子类还是实现该接口的类。 例如
if (obj instanceof Person) {Person p (Person)obj;p.walk();
}5.1 python实现自省的办法
python实现自省有很多方法常用的有 help(),dir(),type(),hasattr()getattr(),setattr(),isinstance(),issubclass(),id(),callable()
5.1.1 help()
help() 函数用于查看函数或模块用途的详细说明。主要在IDE环境下是用接受任何拥有函数或者方法的对象打印出对象所有的函数和文档字符串
如可以直接打印出os模块的帮助文档
import os
help(os)
# Help on module os:
#
# NAME
# os - OS routines for NT or Posix depending on what system were on.
#
# DESCRIPTION
# 后面的省略了也可以是我们自定义的类函数或模块
class Demo:this is a DemoclassVar 0def __init__(self):self.var1 1def output(self):print(self.var1)if __name__ __main__:help(Demo)运行之后会打印出这个类的完整信息
Help on class Demo in module __main__:class Demo(builtins.object)| this is a Demo| | Methods defined here:| | __init__(self)| Initialize self. See help(type(self)) for accurate signature.| | output(self)| | ----------------------------------------------------------------------| Data descriptors defined here:| | __dict__| dictionary for instance variables (if defined)| | __weakref__| list of weak references to the object (if defined)| | ----------------------------------------------------------------------| Data and other attributes defined here:| | classVar 0实例对象会打印出类的信息
函数会打印出帮助文档没有文档会打印none def demoMethods(a):这是一个示例函数:param a: 示例形参:return: Noneprint(a)help(demoMethods)
# Help on function demoMethods in module __main__:# demoMethods(a)
# 这是一个示例函数
# :param a: 示例形参
# :return: None更详细的请看这篇文章
Python-自省机制
5.1.2 dir() dir() 函数不带参数时返回当前范围内的变量、方法和定义的类型列表带参数时返回参数的属性、方法列表。如果参数包含方法__dir__()该方法将被调用。如果参数不包含__dir__()该方法将最大限度地收集参数信息。 dir()
[__builtins__, __doc__, __file__, __loader__, __name__, __package__, __spec__, sys]
dir([])
[__add__, __class__, __contains__, __delattr__, __delitem__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __iadd__, __imul__, __init__, __init_subclass__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __reversed__, __rmul__, __setattr__, __setitem__, __sizeof__, __str__, __subclasshook__, append, clear, copy, count, extend, index, insert, pop, remove, reverse, sort]
5.1.3 hasattr()getattr(),setattr()
class Demo:def __init__(self):self.var1 0self.var2 1if __name__ __main__:demo Demo()if hasattr(demo,var1):setattr(demo,var1,2)print(getattr(demo,var1,not find)) # 2print(getattr(demo,var11,not find)) # not findhasattr()
def hasattr(*args, **kwargs): # real signature unknownReturn whether the object has an attribute with the given name.返回对象是否具有给定名称的属性。This is done by calling getattr(obj, name) and catching AttributeError.这是通过调用getattr(objname)并捕获AttributeError来完成的.passsetattr()
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 vsetattr(x‘y’v)等价于“x.yv”passgetattr()
def getattr(object, name, defaultNone): # known special case of getattrgetattr(object, name[, default]) - valueGet 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 doesntexist; without it, an exception is raised in that case.从对象中获取指定名称的属性getattr(x‘y’)等同于X.Y。如果给定了默认参数则未找到该属性时将返回该参数。如果未指定则会引发异常。pass5.1.4 isinstance()issubclass() help(isinstance)
Help on built-in function isinstance in module builtins:isinstance(obj, class_or_tuple, /)Return whether an object is an instance of a class or of a subclass thereof.返回对象是类的实例还是其子类的实例。A tuple, as in isinstance(x, (A, B, ...)), may be given as the target tocheck against. This is equivalent to isinstance(x, A) or isinstance(x, B)or ... etc.instance类似于type(),只不过type() 不会认为子类是一种父类类型不考虑继承关系。isinstance() 会认为子类是一种父类类型考虑继承关系。 class A:pass a A()isinstance(a,type)
Falseclass B(A):pass bB()isinstance(b,A)
Trueisinstance(int,type)
Trueisinstance(A,type)
Trueisinstance(b,type)
Falseisinstance(True,int)
True可以看出类是type的子类型也验证了前天的元类而布尔是int的子类
而issubclass()则是用来判断一个类是不是另一个类的子类,传入的两个参数都是类名 issubclass(B,A)
True5.1.5 id()和callable()
id(): 用于获取对象的内存地址callable()判断对象是否可以被调用。
5.1.6 type()
这个函数在元类中写过了当传入一个参数时会返回对象的类型这也是python自省中比较常用的方法
5.2 总结
什么是自省
简单来说就是在程序运行过程中能知道对象类型还有属性等的能力
python实现自省的方法
方法作用help()查看函数或模块用途的详细说明dir()返回对象所有属性type()查看对象类型hasattr()查看对象是否有特定属性getattr()得到对象的特定属性seetattr()设置对象的特定属性isinstance()判断一个对象是否是一个已知的类型issubclass()判断一个类是不是另一个类的子类id()返回地址值callable()判断对象是否可调用
参考文章
python面试题
wikipedia Type introspection
Python自省反射指南【转】
在这篇文章中说 在笔者也就是我的概念里自省和反射是一回事当然其实我并不十分确定一定以及肯定… 但是我在维基百科看见了这句话 Introspection should not be confused with reflection, which goes a step further and is the ability for a program to manipulate the values, meta-data, properties and/or functions of an object at runtime. 也就是说自省和反射不是同一回事自省是获取对象类型的能力而反射是操纵对象的值元数据属性和/或函数的能力
Python常用的自省函数
Python-自省机制
Python自省
菜鸟教程