简体   繁体   English

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

[英]How to know attributes vs methods in Python object?

How do you know attributes vs methods in Python object?您如何知道 Python object 中的属性与方法? When using the dir method it only list everything for example dir('string').使用 dir 方法时,它只列出所有内容,例如 dir('string')。

You can test the attributes's type:您可以测试属性的类型:

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)])

prints印刷

[('__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)]     # <<<<<<<<<<

Note that this won't print True for builtin methods that are not explicitly defined in the class definition (or attached later, see __str__ and another_method above).请注意,对于未在 class 定义中明确定义的内置方法(或稍后附加,请参阅上面的__str__another_method ),这不会打印True Note, also, that unlike testing for callable , this actually catches the distinction between methods and callable attributes.另请注意,与对callable的测试不同,这实际上抓住了方法和可调用属性之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python如何知道对象属性(方法)以及如何知道dict的层次结构 - python how to know the object attributes(methods) and how to know the hierarchy of the dict 将对象属性传递给其方法(Python) - Passing object attributes to its methods (Python) Python中是否有一个函数列出特定对象的属性和方法? - Is there a function in Python to list the attributes and methods of a particular object? python中既没有属性也没有方法的对象 - Object that has neither attributes nor methods in python 如何访问一个对象的方法/属性以在 Python 中的另一个对象中使用? - How do I access methods/attributes of one object to use in another object in Python? 在查看dir中列出的属性和方法列表时,您如何知道属性和方法? - How do you know when looking at the list of attributes and methods listed in a dir which are attributes and which are methods? Python语言问题:object()与Function的属性 - Python Language Question: attributes of object() vs Function 如何模拟对象属性以及复杂的字段和方法? - How to mock object attributes and complex fields and methods? Python方法:返回数据与将数据存储在属性中 - Python Methods: Returning data vs Storing data in attributes python 中什么时候将变量传递给方法与读取属性更可取 - When is it preferable to pass variables to methods vs. read attributes in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM