Comments (6)
单例模式 例子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.
@baby5
根据前面的讲解,__instances = {}应该使用单下划线吧
from interview_python.
@baby5
擦,貌似很有道理。
Special cases aren't special enough to break the rules.
python之禅讲到,不要轻易打破规则。
from interview_python.
单下划线命名的变量名是私有变量, 打破这个规则不合适吧(虽然不是严格的私有变量,仍然有办法访问到 逃
from interview_python.
ob = super().__new__(cls)
from interview_python.
单例模式中的装饰器版本有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)
- 资料有一处错误 HOT 3
- 乐观锁悲观锁 导读链接 挂了 HOT 1
- 乐观锁悲观锁链接挂了,建议改用这个链接:https://my.oschina.net/feixuewuhen/blog/800346
- 和小土刀的笔记一模一样 HOT 2
- 对字符串格式化
- 关于 "23 Python里的拷贝"的疑问
- a
- GIL 部分描述有误 HOT 1
- __new__和__init__的区别 HOT 1
- 编程题 第8题 问题描述不够精确
- Gitbook 的链接看不到里面的内容 HOT 3
- 感觉GET和POST区别的解释有点问题
- 13 关于python的重载 应该是可以通过classmethod实现
- 3 @classmethod 和@statimethod 在子类继承上也有区别
- 交叉链表求焦点,修改了一下后面那个解答是错误的 HOT 1
- 有一部分简书的链接404了 可能需要更新一下超链接
- [名词修改]自省建议改成反射
- 15 中的 __new__ 方法是类方法吧 HOT 2
- 这里面应该有不少答案是由英文翻译过来的,而且是非技术人员翻译,很容易被误导,请注意! HOT 1
- 6.字典推导式中有错误 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interview_python.