自省的意思,从书面来讲就是自我反省,自我剖析。

联机帮助(help)

当我们对某一个关键字和模块不是很熟悉,这时候就可以向python发送 救命(help),首先输入help()函数,进行help命令继续输入想要查找的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> help()
help>__dict__ #会得到官方说明
Help on dict object:

__dict__ = class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
-- More -- #点击 enter 键可以继续知道详情

>>> help("str") #同样也可以查到, 适用于模块查询
Help on class str in module builtins:

class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
-- More --

dir()

上面的使用我们已经知道了查找的内容,如果连名字都不知道怎么办呢,这时候可以使用dir函数,可以查看模块以及所有对象类型,还是比较好用的。

他返回传递给他的任何对象和属性名称经过排序列表。

如果不传入参数,他默认查找当前的作用域。

1
2
3
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>>

适用于所有的对象类型,比如:

  • 字符串
  • 数字
  • 列表
  • 类对象
  • 类方法
  • 元祖
  • 字典
  • 函数
  • 定制类
  • 传递数字
1
2
3
4
5
6
7
>>> dir(int(5))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__'
, '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__po
s__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift_
_', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag'
, 'numerator', 'real', 'to_bytes']
>>>

__doc__文档描述

它包含了描述对象的注释,用__doc__

1
2
3
4
5
>>> dir.__doc__
"dir([object]) -> list of strings\n\nIf called without an argument, return the names in the current scope.\nElse, return an alphabetized list of names comprising (some of) the attributes\nof the given object, and of
attributes reachable from it.\nIf the object supplies a method named __dir__, it will be used; otherwise\nthe default dir() logic is used and returns:\n for a module object: the module's attributes.\n for a class o
bject: its attributes, and recursively the attributes\n of its bases.\n for any other object: its attributes, its class's attributes, and\n recursively the attributes of its class's base classes."
>>>

检查python对象

我们通过以下几点进行查找。

  • 对象的名称用 __name__

  • 对象的类型用type()最好用sinstance()

  • 对象知道什么用id()

  • 对象能做些什么用callable()

  • 对象的父对象是谁用issubclass()

对象名称:

1
2
3
4
5
def a():
pass
b=a
print(b.__name__) #始终都会找到最初是的名称,中间的转化的不算。
a

对象类型

使用type(),这里不讲述了,在对象,类,type关系已经详细讲过了,他会返回一个对象类型,如下:

1
2
3
4
5
6
7
8
9
10
def a():
pass
b=a


print(type(a))
print(type(1))
#打印结果
<class 'function'>
<class 'int'>

对象知道什么用id()

1
2
3
4
5
6
7
8
9
10
11
12
a=[1,2,3]
b=[1,2,3]

c=a
print(id(a))
print(id(b))
print(id(c))

print(a is b)
print(c is a)

print((a==b))

看结果,a is b比较用的是ID,a==b比较用的是value。

1
2
3
4
5
6
2933462198600
2933461439048
2933462198600
False
True
True

对象能做些什么用callable()

这里指函数或方法的对象的可调用性。

1
2
3
4
5
6
7
8
def b():
pass

print(callable(b))
print(callable(str)) #str 是一个内置方法,是可以调用。
#打印结果
True
True

对象的父对象是谁用issubclass()

1
2
3
4
5
6
7
8
9
10
11
class A:
pass
class B(A):
pass

class C(B):
pass

print(C.__bases__) #查找父类
print(C.__mro__)# 寻找MRO 路线
print(issubclass(C,A))#比较是不是属于父类

打印结果

1
2
3
(<class '__main__.B'>,)
(<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
True