
在命令行中:
C:\Users\admin>python
Python 382 (tags/v382:7b3ab59, Feb 25 2020, 22:45:29) [MSC v1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information
>>> import numpy
>>> help(numpy)
Help on package numpy:
NAME
numpy
DESCRIPTION
NumPy
=====
Provides
1 An array object of arbitrary homogeneous items
2 Fast mathematical operations over arrays
3 Linear Algebra, Fourier Transforms, Random Number Generation
How to use the documentation
----------------------------
Documentation is available in two forms: docstrings provided
with the code, and a loose standing reference guide, available from
-- More --
要在 Python 中查看和打印局部变量和全局变量的列表,可以使用 和 内置函数。这些函数返回一个字典,分别包含当前作用域中所有局部变量和全局变量的名称和值。然后,您可以使用字典的方法循环访问键值对并将其打印到输出中。locals()globals()items()
下面是如何使用 and 函数在 Python 中打印局部变量和全局变量的示例:locals()globals()
此代码定义函数中的全局变量和局部变量。然后,它使用 and 函数分别打印局部变量和全局变量的列表。运行此代码时,它将输出以下内容:global_varlocal_varlocals()globals()
如您所见,该函数返回一个包含局部变量的字典,而该函数返回一个包含全局变量 、 和 的字典。您可以使用此方法在 Python 中查看和打印任何范围内的局部变量和全局变量列表。locals()local_varglobals()__name__global_varprint_vars
回答不易望请采纳
Python: 测试函数是否被调用
# helper class defined elsewhere
class CallLogger(object):
def __init__(self, meth):
selfmeth = meth
selfwas_called = False
def __call__(self, code=None):
selfmeth()
selfwas_called = True
然后assert CallLogger的was_called为True就行了。但是这样的Callable不是个函数:
isinstance(object, typesFunctionType) # Callable will be False
对于这种Callable获取参数个数需要用:
inspectgetargspec(fn__call__)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)