zh826256645
2019-01-19 10:55:19 +08:00
class LocalProxy(object):__slots__ = ('__local', '__dict__', '__name__', '__wrapped__')
In [3] used 0.0312 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 33.28 MiB
In [4]: lp = LocalProxy()
In [4] used 0.0391 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 33.32 MiB
In [5]: lp
Out[5]: <__main__.LocalProxy at 0x10324e5f0>
In [5] used 0.0117 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 33.33 MiB
In [6]: class LocalProxy(object):
...: pass
...:
In [6] used 0.4688 MiB RAM in 0.11s, peaked 0.00 MiB above current, total RAM usage 33.80 MiB
In [7]: lp = LocalProxy()
In [7] used 0.0508 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 33.85 MiB
In [8]: lp
Out[8]: <__main__.LocalProxy at 0x103349110>
In [8] used 0.0039 MiB RAM in 0.11s, peaked 0.00 MiB above current, total RAM usage 33.86 MiB
--------------------------------------- 看看楼上老哥的例子 -------------------------------------------------
In [9]: class C(object):
...: __slots__ = ('x', 'y', '__dict__')
...: def __init__(self):
...: self.x = 1
...: self.y = 2
...:
In [9] used 0.2305 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 34.09 MiB
In [10]: c = C()
In [10] used 0.0000 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 34.09 MiB
In [11]: c
Out[11]: <__main__.C at 0x103147c68>
In [11] used 0.0039 MiB RAM in 0.11s, peaked 0.00 MiB above current, total RAM usage 34.09 MiB
In [12]: class C(object):
...: def __init__(self):
...: self.x = 1
...: self.y = 2
...:
In [12] used 0.0430 MiB RAM in 0.11s, peaked 0.00 MiB above current, total RAM usage 34.13 MiB
In [13]: c = C()
In [13] used 0.0000 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 34.13 MiB
In [14]: c
Out[14]: <__main__.C at 0x10335d210>
In [14] used 0.0000 MiB RAM in 0.10s, peaked 0.00 MiB above current, total RAM usage 34.13 MiB
事实证明确实是为了尽可能的省内存,想省内存,但是又不想丢弃 __dict__,__weakref__ 这两个功能
只能说细真的细