📜  Python中的代码自省

📅  最后修改于: 2022-05-13 01:54:48.021000             🧑  作者: Mango

Python中的代码自省

自省是一种在运行时确定对象类型的能力。 Python中的一切都是一个对象。 Python中的每个对象都可能具有属性和方法。通过使用自省,我们可以动态地检查Python对象。代码自省用于检查类、方法、对象、模块、关键字并获取有关它们的信息,以便我们可以使用它。自省揭示了有关程序对象的有用信息。 Python作为一种动态的、面向对象的编程语言,提供了强大的内省支持。 Python 对自省的支持深入而广泛地贯穿于整个语言。
Python提供了一些用于代码自省的内置函数。它们是:
1. type() :该函数返回对象的类型。

# Python program showing
# a use of type function
  
import math
  
# print type of math
print(type(math))
   
# print type of 1 
print(type(1))
  
# print type of "1"
print(type("1"))
  
# print type of rk
rk =[1, 2, 3, 4, 5, "radha"]
  
print(type(rk))
print(type(rk[1]))
print(type(rk[5]))

输出:








2. dir() :此函数返回与该对象关联的方法和属性的列表。

# Python program showing
# a use of dir() function
  
import math
rk =[1, 2, 3, 4, 5]
  
# print methods and attributes of rk
print(dir(rk))
rk =(1, 2, 3, 4, 5)
  
# print methods and attributes of rk
print(dir(rk))
rk ={1, 2, 3, 4, 5}
  
print(dir(rk))
print(dir(math))
输出:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__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']
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']


3. str() :此函数将所有内容转换为字符串。

# Python program showing
# a use of str() function
  
a = 1
print(type(a))
  
# converting integer
# into string
a = str(a)
print(type(a))
  
s =[1, 2, 3, 4, 5]
print(type(s))
  
# converting list
# into string
s = str(s)
print(type(s))

输出:






4. id() :这个函数返回一个对象的特殊id。

# Python program showing
# a use of id() function
   
import math
a =[1, 2, 3, 4, 5]
   
# print id of a
print(id(a))
b =(1, 2, 3, 4, 5)
   
# print id of b
print(id(b))
c ={1, 2, 3, 4, 5}
   
# print id of c
print(id(c))
print(id(math))

输出:

139787756828232
139787757942656
139787757391432
139787756815768

代码自省的方法

FunctionDescription
help()It is used it to find what other functions do
hasattr()Checks if an object has an attribute
getattr()Returns the contents of an attribute if there are some.
repr()Return string representation of object
callable()Checks if an object is a callable object (a function)or not.
issubclass()Checks if a specific class is a derived class of another class.
isinstance()Checks if an objects is an instance of a specific class.
sys()Give access to system specific variables and functions
__doc__Return some documentation about an object
__name__Return the name of the object.