V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
vtoexsir
V2EX  ›  Python

有没有类似 dir(obj)的方法,可以查看 obj 的全部属性和法,但要将属性和方法区分开?

  •  
  •   vtoexsir · 2017-06-16 09:50:04 +08:00 · 2024 次点击
    这是一个创建于 2505 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如有如下的类(当然只定义方法也是可以的):

    class A():
    	a = '这是属性'
    
    	def d(self, var):
        	print('这是方法')
    
    	def dd(self):
       		print('这是没有参数的方法')
    
    	aa = dd  #也算属性吧
    
    print(dir(A))
    # =>['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'aa', 'd', 'dd']
    

    dir(obj),虽然能获得['a', 'aa', 'd', 'dd'] ,但是不能区分是方法还是属性。
    有什么方法或者现成的库可以将方法和属性分开呢?
    多谢您回复!

    7 条回复    2017-06-17 15:51:13 +08:00
    XYxe
        1
    XYxe  
       2017-06-16 10:22:20 +08:00   ❤️ 2
    guyskk
        2
    guyskk  
       2017-06-16 10:38:06 +08:00 via Android
    Python 里方法就是可调用的属性,上面的 aa 和 dd 是完全一样的,没法区分。a 和 d 可以用 getattr 取出来在判断是不是 callable 区分。
    congeec
        3
    congeec  
       2017-06-16 11:40:42 +08:00 via iPhone
    注意一下 @property
    NoAnyLove
        4
    NoAnyLove  
       2017-06-16 11:42:18 +08:00
    `aa`应该算作 function 了。另外,`inspect`模块也可以用:

    ```
    >>> import inspect
    >>> inspect.getmembers(A, inspect.isfunction)
    [('aa', <function __main__.A.dd>),
    ('d', <function __main__.A.d>),
    ('dd', <function __main__.A.dd>)]
    ```

    ```
    >>> inspect.getmembers(A, lambda x: not callable(x))
    [('__dict__',
    mappingproxy({'__dict__': <attribute '__dict__' of 'A' objects>,
    '__doc__': None,
    '__module__': '__main__',
    '__weakref__': <attribute '__weakref__' of 'A' objects>,
    'a': '这是属性',
    'aa': <function __main__.A.dd>,
    'd': <function __main__.A.d>,
    'dd': <function __main__.A.dd>})),
    ('__doc__', None),
    ('__module__', '__main__'),
    ('__weakref__', <attribute '__weakref__' of 'A' objects>),
    ('a', '这是属性')]
    ```
    vtoexsir
        5
    vtoexsir  
    OP
       2017-06-16 20:35:04 +08:00
    非常感谢各位!
    pdir 内部也是调用 inspect。
    谢谢!
    mingyun
        6
    mingyun  
       2017-06-17 00:15:58 +08:00
    @XYxe nice
    yucongo
        7
    yucongo  
       2017-06-17 15:51:13 +08:00
    pdir2 不错,感谢一下 XYxe。也感谢一下提问者,我也在有过同样的疑问,比如 pathlib 里的 Path 不知道哪个是属性、哪个是方法……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5529 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 06:43 · PVG 14:43 · LAX 23:43 · JFK 02:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.