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
alittlecode
V2EX  ›  Python

有没有方法获取到一个 py 文件中所有方法的名称,除了使用正则匹配

  •  
  •   alittlecode · 2021-04-02 17:49:28 +08:00 · 1986 次点击
    这是一个创建于 1113 天前的主题,其中的信息可能已经有所发展或是发生改变。
    8 条回复    2021-04-02 21:21:50 +08:00
    imn1
        1
    imn1  
       2021-04-02 17:59:53 +08:00
    一切皆为对象
    import re
    dir(re)
    alittlecode
        2
    alittlecode  
    OP
       2021-04-02 18:03:40 +08:00
    @imn1 这样把引入的变量什么的都包含了
    est
        3
    est  
       2021-04-02 18:15:55 +08:00
    ast 模块
    demobin
        4
    demobin  
       2021-04-02 18:18:48 +08:00
    for item in re.__all__:
    print(item, hasattr(getattr(re, item), '__call__'))
    imn1
        5
    imn1  
       2021-04-02 18:21:21 +08:00
    @alittlecode #2
    你自己多想一步不行么?有个内置函数叫 callable()

    如果你想更“正式”一点,以前有个包叫 optparse,不过已经废弃了
    现在可以用 inspect,typing 之类的自己组织
    chionetw5
        6
    chionetw5  
       2021-04-02 19:36:05 +08:00
    通过 importlib
    xiaolinjia
        7
    xiaolinjia  
       2021-04-02 21:16:01 +08:00
    inspect.getmembers(promotions, inspect.isfunction)
    dingwen07
        8
    dingwen07  
       2021-04-02 21:21:50 +08:00 via iPhone
    inspect 模块
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2612 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:16 · PVG 12:16 · LAX 21:16 · JFK 00:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.