Giter Site home page Giter Site logo

Comments (6)

lambdaplus avatar lambdaplus commented on September 27, 2024

单例模式 例子2 共享属性 也有问题。

Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 12:22:00) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: class Borg(object):
   ...:     _state = {}
   ...:     def __new__(cls, *args, **kw):
   ...:         ob = super(Borg, cls).__new__(cls, *args, **kw)
   ...:         ob.__dict__ = cls._state
   ...:         return ob
   ...: 
   ...: class MyClass2(Borg):
   ...:     a = 1
   ...:     

In [2]: a = MyClass2()

In [3]: b = MyClass2()

In [4]: id(a)
Out[4]: 140280935744904

In [6]: id(b)
Out[6]: 140280935776096

In [7]: c = Borg()

In [8]: d = Borg()

In [9]: id(c)
Out[9]: 140280935117376

In [10]: id(d)
Out[10]: 140280935073888

from interview_python.

jackupdown avatar jackupdown commented on September 27, 2024

@baby5
根据前面的讲解,__instances = {}应该使用单下划线吧

from interview_python.

jackupdown avatar jackupdown commented on September 27, 2024

@baby5
擦,貌似很有道理。
Special cases aren't special enough to break the rules.
python之禅讲到,不要轻易打破规则。

from interview_python.

MarcelArthur avatar MarcelArthur commented on September 27, 2024

单下划线命名的变量名是私有变量, 打破这个规则不合适吧(虽然不是严格的私有变量,仍然有办法访问到 逃

from interview_python.

andersonshao avatar andersonshao commented on September 27, 2024
ob = super().__new__(cls)

from interview_python.

z8seeker avatar z8seeker commented on September 27, 2024

单例模式中的装饰器版本有bug

当被装饰的类中 __init__() 方法有初始化参数时,将报 TypeError 异常。建议更改为:

def singleton(cls):
    instances = {}
    def getinstance(*args, **kw):
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]
    return getinstance

from interview_python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.