forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcached_type.txt
More file actions
38 lines (26 loc) · 1.25 KB
/
cached_type.txt
File metadata and controls
38 lines (26 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
..
Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative
Commons Attribution-ShareAlike 3.0 Unported License, with attribution to
"Ram Rachum at ram.rachum.com" including link. The license may be obtained
at http://creativecommons.org/licenses/by-sa/3.0/
.. _topics-caching-cached-type:
:class:`caching.CachedType`
===========================
A class that automatically caches its instances
-----------------------------------------------
Sometimes you define classes whose instances hold absolutely no state on them,
and are completey determined by the arguments passed to them. In these cases
using :class:`caching.CachedType` as a metaclass would cache class instances,
preventing more than one of them from being created:
>>> from python_toolbox import caching
>>>
>>> class A(metaclass=caching.CachedType):
... def __init__(self, a=1, b=2):
... self.a = a
... self.b = b
Now every time you create an instance, it'll be cached:
>>> my_instance = A(b=3)
And the next time you'll create an instance with the same arguments:
>>> another_instance = A(b=3)
No instance will be actually created; the same instance from before will be used:
>>> assert another_instance is my_instance