partial
partial函数的主要功能是通过固定给定函数的参数来创建一个新的函数。看一个官网的例子:
>>>from functools import partial
>>>basetwo = partial(int, base = 2)
>>>basetwo.__doc__ = 'Convert base 2 string to an int'
>>>basetwo('10010')
18
这里实现的功能就是通过固定int函数的base为2,把一个二进制字符串转换为int。
id
python中一切皆对象,变量中存的是对象的引用。id()函数返回的是对象在生命周期内的内存地址。可以用来唯一标志一个对象,但是生命周期不重叠的对象的id()是可能相同的。
vars
vars()函数返回的是对象的属性和属性值的字典对象。看下面的例子:
>>>class Dog:
>>> color = "black"
>>> length = 76
>>> name = "sanhei"
>>>
>>>vars(Dog)
{'__module__': '__main__', 'color': 'black', 'length': 76, 'name': 'sanhei', '__dict__': <attribute '__dict__' of 'Dog' objects>, '__weakref__': <attribute '__weakref__' of 'Dog' objects>, '__doc__': None}
locals
local()函数返回当前位置的全部局部变量。通过下面的例子可以看出该函数的使用方法。
>>>b = 3
>>>c = 4
>>>def test_func():
>>> a = 1
>>> global b
>>> b = 2
>>> c = 5
>>>print("local in func:", locals())
>>>
>>>test_func()
>>>print(locals())
>>>print(globals())
local in func: {'a': 1, 'c': 5}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f8436f67f90>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'test_local_global.py', '__cached__': None, 'b': 2, 'c': 4, 'test_func': <function test_func at 0x7f8436ee05f0>}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f8436f67f90>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'test_local_global.py', '__cached__': None, 'b': 2, 'c': 4, 'test_func': <function test_func at 0x7f8436ee05f0>}
globals
globals()函数与locals()函数对应,它返回的是全局的名字空间,这里要注意的是对globals返回的结果进行修改会直接影响到全局变量的取值。而locals的返回值只是一个拷贝,不会影响原始值。
dir
dir()函数是python较常用的函数,它的返回值是查询对象的所有属性和方法名的列表。
>>>class MyClass:
>>> def __init__(self):
>>> aaa = "aaa"
>>> def func1():
>>> pass
>>>print(dir(MyClass))
>>>print(dir())
>>>print(dir(list))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'func1']
['MyClass', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
['__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']