如何知道 Python 对象中的属性与方法?

Dja*_*now 3 python python-3.x

您如何知道 Python 对象中的属性与方法?使用 dir 方法时,它只列出所有内容,例如 dir('string')。

GPh*_*ilo 5

您可以测试属性的类型:

from types import MethodType
from pprint import pprint

class A(object):
  def __init__(self) -> None:
    self._field = 3
    self._callable_field = lambda x: x
  
  def my_method(self):
    pass

  @classmethod
  def my_static_method(cls):
    pass

  def __str__(self) -> str:
    return repr(self)

A.another_method = lambda self: None

a = A()
pprint([(d, type(getattr(a,d)) is MethodType) for d in dir(a)])
Run Code Online (Sandbox Code Playgroud)

印刷

[('__class__', False),
 ('__delattr__', False),
 ('__dict__', False),
 ('__dir__', False),
 ('__doc__', False),
 ('__eq__', False),
 ('__format__', False),
 ('__ge__', False),
 ('__getattribute__', False),
 ('__gt__', False),
 ('__hash__', False),
 ('__init__', True),
 ('__init_subclass__', False),
 ('__le__', False),
 ('__lt__', False),
 ('__module__', False),
 ('__ne__', False),
 ('__new__', False),
 ('__reduce__', False),
 ('__reduce_ex__', False),
 ('__repr__', False),
 ('__setattr__', False),
 ('__sizeof__', False),
 ('__str__', True),              # <<<<<<<<<<
 ('__subclasshook__', False),
 ('__weakref__', False),
 ('_callable_field', False),     # <<<<<<<<<<
 ('_field', False),              # <<<<<<<<<<
 ('another_method', True),       # <<<<<<<<<<
 ('my_method', True),            # <<<<<<<<<<
 ('my_static_method', True)]     # <<<<<<<<<<
Run Code Online (Sandbox Code Playgroud)

请注意,True对于未在类定义中明确定义的内置方法(或稍后附加,请参阅__str__another_method以上),这不会打印。另请注意,与测试 for 不同callable,这实际上抓住了方法和可调用属性之间的区别。