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

Python 中, 仿照经典代码实现单例, 却出现了不是单例的的状态, 代码哪里出错了 ?

  •  
  •   wentian · 2017-03-08 20:14:08 +08:00 · 1851 次点击
    这是一个创建于 2613 天前的主题,其中的信息可能已经有所发展或是发生改变。
    14 条回复    2017-03-09 15:15:24 +08:00
    junnplus
        1
    junnplus  
       2017-03-08 20:21:09 +08:00 via iPhone
    python 中直接模块级常量就是单例
    wentian
        2
    wentian  
    OP
       2017-03-08 20:27:41 +08:00
    @junnplus 我的第一处代码是仿照经典实现, 而且自己也看不少书, 都是推荐这种写法

    我的疑惑在于: 是不是我写错了 ?
    Kilerd
        3
    Kilerd  
       2017-03-08 20:38:09 +08:00
    单例模式 建议用 __new__,不是用 __init__
    amoyiki
        4
    amoyiki  
       2017-03-08 20:48:25 +08:00 via iPhone
    应该用__new__只创建一个实例
    junnplus
        5
    junnplus  
       2017-03-08 20:56:36 +08:00
    @wentian 你这是那里看到的推荐写法,“看了不少书”?你可以列出书名。另外楼上都是正解
    junnplus
        6
    junnplus  
       2017-03-08 20:57:35 +08:00
    你这种写法只是共用一个类变量,但是每次都实例化 BackgroundScheduler
    mooncakejs
        7
    mooncakejs  
       2017-03-08 20:59:51 +08:00 via iPhone
    @junnplus 正解。
    junnplus
        8
    junnplus  
       2017-03-08 21:00:42 +08:00
    还有,看了你给的链接 https://github.com/faif/python-patterns/blob/master/creational/borg.py
    人家写的没错,通过继承共用类变量来实现单例
    wentian
        9
    wentian  
    OP
       2017-03-08 21:33:24 +08:00
    @junnplus
    是不是我还要继承下( 我原以为那个继承, 就是做做样子的, 直接使用基类不就完了 :P )
    但是, 如果我使用 str 之类的, 就和他的完全保持一致了



    使用 __new__ , 见过, 但是没有深入了解其原理过


    清一色的 Borg 推荐: https://www.google.com/search?q=python+singleton&oq=python+singleton+&aqs=chrome..69i57.6145j0j7&sourceid=chrome&ie=UTF-8


    但是都加了一个继承, 看起来那个继承, 不是我认为「多余的」


    :P
    wentian
        10
    wentian  
    OP
       2017-03-08 21:35:44 +08:00
    @junnplus

    反正各种解决方法就是了, 大家也木有一个统一的意见, 貌似也木有最佳实践了
    junnplus
        11
    junnplus  
       2017-03-08 21:55:47 +08:00
    @wentian python 中最佳实践就是模块级常量
    dsg001
        12
    dsg001  
       2017-03-08 22:16:40 +08:00
    模块,简单安全
    jingniao
        13
    jingniao  
       2017-03-08 22:52:39 +08:00 via Android
    模块变量,简单的多线程锁就这么干的,跑的挺欢的
    lunaticus7
        14
    lunaticus7  
       2017-03-09 15:15:24 +08:00
    可以试下这个单例装饰器,定义类的时候加在前面就行

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-


    class SingletonDecorator:
    def __init__(self, klass):
    self.klass = klass
    self.instance = None

    def __call__(self, *args, **kwds):
    if self.instance is None:
    self.instance = self.klass(*args, **kwds)
    return self.instance
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2225 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:45 · PVG 12:45 · LAX 21:45 · JFK 00:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.