python - python中的dir函数

标签 python

help(dir):
dir([object]) -> list of strings        
If called without an argument, return the names in the current scope.  
Else, return an alphabetized list of names comprising (some of) the attributes  
of the given object, and of attributes reachable from it.  
If the object supplies a method named __dir__, it will be used; otherwise  
the default dir() logic is used and returns:  
  for a module object: the module's attributes.  
  for a class object:  its attributes, and recursively the attributes  
    of its bases.  
  for any other object: its attributes, its class's attributes, and  
    recursively the attributes of its class's base classes.

我发现dir内置函数的帮助文件可能有问题,例如:

class  AddrBookEntry(object):
   'address book entry class'
   def __init__(self,nm,ph):
     self.name=nm
     self.phone=ph
   def updatePhone(self,newph):
     self.phone=newph
     print 'Updated phone # for :' ,self.name

dir(AddrBookEntry('tom','123'))
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'phone', 'updatePhone']

1.dir()可以列出对象的方法,而不仅仅是属性
updatePhone 是方法,不是属性。

2.我怎么知道dir()输出中哪个是属性,哪个是方法?

最佳答案

  1. Python 中的方法属性。

  2. 检查它们的各种属性。方法具有 im_* 属性。

关于python - python中的dir函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513892/

相关文章:

python - django - DRF 中的登录 View

python - 获取 Linux Python 的通用操作系统名称

python - 找不到 setup.py 中的 File.open(readme)

python - Pandas:根据 bool 列表/字典替换数据框列

python - 如何解决TypeError : on_delete must be callable on Django models?

python - Python numpy 中缺失值的奇怪计时

python - 在 Tensorflow-lite 中输入具有动态尺寸的图像

python - django 1.7、python 3.4 : Microsoft Visual Studio 10. 0\\VC\\BIN\\cl.exe' 上的 mysql-python 错误

python - 安装 graphviz 和 pydotplus 后找不到 IPython 3.5 GraphViz 的可执行文件

java - 每第 n 次优化时进行距离检查