python:singleton
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
python:singleton [2017/06/01 04:37] – rex8312 | python:singleton [2024/03/23 02:42] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 2: | 줄 2: | ||
<code python> | <code python> | ||
+ | # class Singleton(type): | ||
+ | # | ||
+ | # def __call__(cls, | ||
+ | # if cls not in cls._instances: | ||
+ | # | ||
+ | # | ||
+ | | ||
class Singleton(type): | class Singleton(type): | ||
- | | + | _instances = {} |
- | + | | |
- | | + | |
- | if cls not in cls._instances: | + | |
- | cls._instances[cls] = super(Singleton, cls).__call__(*args, | + | |
- | return cls._instances[cls] | + | |
| | ||
줄 27: | 줄 32: | ||
print(MyClass().x) | print(MyClass().x) | ||
</ | </ | ||
- | | + | |
- | * http:// | + | * https:// |
+ | |
python/singleton.1496291852.txt.gz · 마지막으로 수정됨: (바깥 편집)