当我尝试使用自省功能查看在threading.Lock上可用的方法时,我看不到期望的结果。

具体来说,我看不到获取,释放或锁定。为什么是这样?

这是我看到的内容:

>>> dir (threading.Lock)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__str__']

最佳答案

你这样做是错的。 threading.Lock不是对象。

>>> import threading
>>> threading.Lock
<built-in function allocate_lock>
>>> type(threading.Lock)
<type 'builtin_function_or_method'>
>>> x=threading.Lock()
>>> type(x)
<type 'thread.lock'>
>>> dir(x)
['__enter__', '__exit__', 'acquire', 'acquire_lock', 'locked', 'locked_lock', 'release', 'release_lock']
>>>

关于python - python内省(introspection)不显示Lock函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/394300/

10-12 20:00