V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Apr 2, 2021 · 2994 views
    This topic created in 1861 days ago, the information mentioned may be changed or developed.
    8 replies    2021-04-02 21:21:50 +08:00
    imn1
        1
    imn1  
       Apr 2, 2021
    一切皆为对象
    import re
    dir(re)
    alittlecode
        2
    alittlecode  
    OP
       Apr 2, 2021
    @imn1 这样把引入的变量什么的都包含了
    est
        3
    est  
       Apr 2, 2021
    ast 模块
    demobin
        4
    demobin  
       Apr 2, 2021
    for item in re.__all__:
    print(item, hasattr(getattr(re, item), '__call__'))
    imn1
        5
    imn1  
       Apr 2, 2021
    @alittlecode #2
    你自己多想一步不行么?有个内置函数叫 callable()

    如果你想更“正式”一点,以前有个包叫 optparse,不过已经废弃了
    现在可以用 inspect,typing 之类的自己组织
    chionetw5
        6
    chionetw5  
       Apr 2, 2021
    通过 importlib
    xiaolinjia
        7
    xiaolinjia  
       Apr 2, 2021
    inspect.getmembers(promotions, inspect.isfunction)
    dingwen07
        8
    dingwen07  
       Apr 2, 2021 via iPhone
    inspect 模块
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1162 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 23:18 · PVG 07:18 · LAX 16:18 · JFK 19:18
    ♥ Do have faith in what you're doing.