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

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

  •  
  •   zforrest · 2018-06-11 22:14:26 +08:00 · 2003 次点击
    这是一个创建于 2143 天前的主题,其中的信息可能已经有所发展或是发生改变。
    4 条回复    2018-06-12 14:06:10 +08:00
    conn4575
        1
    conn4575  
       2018-06-12 06:21:59 +08:00 via Android
    单机就是个伪全局,可以使用 classmethed
    xpresslink
        2
    xpresslink  
       2018-06-12 09:24:02 +08:00
    这个对于 Python 有省事简单方法,在 python 中把要单例的东西放到一个文件(模块)中,然后所有调用者都直接 import 就是天然单例了。
    闲得没事可以用__new__方法,创建好类的全局实例。在实例化时都返回那个全局实例就可以了。
    zforrest
        3
    zforrest  
    OP
       2018-06-12 09:32:22 +08:00
    @xpresslink 这个样子哇。那懂了。
    @conn4575 get
    Marmot
        4
    Marmot  
       2018-06-12 14:06:10 +08:00
    import 是天然的单列
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5518 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:35 · PVG 09:35 · LAX 18:35 · JFK 21:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.