Python reload 对单例对象的影响 - V2EX
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
towry

Python reload 对单例对象的影响

  •  
  •   towry 2021 年 1 月 23 日 2481 次点击
    这是一个创建于 1919 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假如在 python 中使用了单例模式,维护了一个变量,那么在模块被 reload 后,这个变量会变吗?
    2 条回复    2021-01-24 01:56:17 +08:00
    hareandlion
        1
    hareandlion  
       2021 年 1 月 23 日   1
    这要看单例模式的对象是在哪里创建的了吧,一般情况下单例模式所引用的对象是在被 reload 的 module 里初始化的,那 reload 会创建新的对象,而如果单例引用的是存在内存里类似 int 的基本类型对象,reload 之后依然会引用原本的对象。

    si_cls.py
    ```
    #!/bin/python
    # coding: utf-8

    ph = {'ab': 2}
    # ph = 32


    class SI:
    def __new__(cls, *args, **kwargs):
    return ph

    def id(self):
    return id(self)


    si_instance = SI()
    ```

    test.py
    ```
    #!/bin/python
    # coding: utf-8

    import si_cls
    from importlib import reload

    s1 = si_cls.SI()
    s2 = si_cls.SI()

    print(f'id(s1)={id(s1)}, id(s2)={id(s2)}, id(si_instance)={id(si_cls.si_instance)}')

    reload(si_cls)

    s1 = si_cls.SI()
    s2 = si_cls.SI()

    print(f'id(s1)={id(s1)}, id(s2)={id(s2)}, id(si_instance)={id(si_cls.si_instance)}')

    ```
    abersheeran
        2
    abersheeran  
       2021 年 1 月 24 日 via Android   2
    巧了。以前研究过相关的东西写了一篇博客留作记录。你看看吧。https://aber.sh/articles/Python-Reload/

    当时写的可能比较乱,如果有没看懂的部分可以文章下发评论问。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2963 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 14:35 PVG 22:35 LAX 07:35 JFK 10:35
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86