推荐学习书目
› 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
coolair
V2EX  ›  Python

Python 下有没有什么库结合了 import_string 和 getattr 的功能?

  •  
  •   coolair · Dec 30, 2024 · 6044 views
    This topic created in 689 days ago, the information mentioned may be changed or developed.

    比如 Django 的 import_string:

    from django.utils.module_loading import import_string
    
    cls = import_string('path.to.module.Class')
    func = import_string('path.to.module.func')
    var = import_string('path.to.module.var')
    

    只能到 module 这一级,如果想获取到 Class 的 var 或者 func ,就不行了,比如:

    func = advanced_import_string('path.to.module.Class.func')
    var = advanced_import_string('path.to.module.Class.var')
    

    有现成的库可以用的吗?

    8 replies  •  2025-01-09 16:52:32 +08:00
    nagisaushio
    nagisaushio
    Dec 30, 2024 via Android
    1
    这自己写个函数就能解决吧
    tf2
    tf2
    Dec 30, 2024
    2
    import_string 一时爽,依赖排查火葬场。
    coolair
    coolair
    OP
    Dec 30, 2024
    3
    解决了,使用 pydoc 的 locate 完美,不用自己造轮子了。
    qW7bo2FbzbC0
    qW7bo2FbzbC0
    Dec 30, 2024
    4
    这种黑科技看着就头大
    lijiachang
    lijiachang
    Dec 30, 2024
    5
    IDE 看了就摇头
    zhzy
    zhzy
    Dec 31, 2024
    6
    好奇什么需求必须要用到这种黑科技
    guanhui07
    guanhui07
    Dec 31, 2024
    7
    排查火葬场 高级防御性编程
    izoabr
    izoabr
    Jan 9, 2025
    8
    importlib 呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1057 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:05 · PVG 07:05 · LAX 15:05 · JFK 18:05
    ♥ Do have faith in what you're doing.