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

想问一下问题。在 ios 开发里面会将一些 model 封装成单例进行使用。 Python 有类似额操作么。

  •  
  •   zforrest · Jun 11, 2018 · 3077 views
    This topic created in 3033 days ago, the information mentioned may be changed or developed.
    4 replies  •  2018-06-12 14:06:10 +08:00
    conn4575
        1
    conn4575  
       Jun 12, 2018 via Android
    单机就是个伪全局,可以使用 classmethed
    xpresslink
        2
    xpresslink  
       Jun 12, 2018
    这个对于 Python 有省事简单方法,在 python 中把要单例的东西放到一个文件(模块)中,然后所有调用者都直接 import 就是天然单例了。
    闲得没事可以用__new__方法,创建好类的全局实例。在实例化时都返回那个全局实例就可以了。
    zforrest
        3
    zforrest  
    OP
       Jun 12, 2018
    @xpresslink 这个样子哇。那懂了。
    @conn4575 get
    5oiR5piv5YK76YC8
        4
    5oiR5piv5YK76YC8  
       Jun 12, 2018
    import 是天然的单列
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2397 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:36 · PVG 15:36 · LAX 00:36 · JFK 03:36
    ♥ Do have faith in what you're doing.