デザインパターンの勉強をしています。
下記のコードで二回目のIBorg()のインスタンスを作成した際に、すでにself._shared_state
に{'state': 'init'}
がなぜ格納されているのでしょうか?
python
1class Borg: 2 _shared_state = {} 3 def __init__(self): 4 print(f"self._shared_state: {self._shared_state}") 5 self.__dict__ = self._shared_state 6 7class IBorg(Borg): 8 def __init__(self): 9 Borg.__init__(self) 10 self.state = 'init' 11 12 def __str__(self): 13 return self.state 14 15if __name__ == "__main__": 16 i1 = IBorg() 17 print(f"i1: {i1}") 18 i2 = IBorg() 19 print(f"i2: {i2}")
アウトプット
python
1$ python xxx.py 2self._shared_state: {} 3i1: init 4self._shared_state: {'state': 'init'} 5i2: init
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/30 07:42
2021/04/30 07:45
2021/04/30 07:51 編集
2021/04/30 08:37
2021/04/30 11:15
2021/04/30 16:19
2021/04/30 17:18
2021/05/01 01:24 編集
2021/05/01 20:32 編集
2021/05/01 09:02 編集
2021/05/02 04:47
2021/05/02 12:39
2021/05/03 22:34
2021/05/07 12:05 編集
2021/05/06 12:39 編集
2021/05/06 13:29
2021/05/06 15:30
2021/05/07 14:20 編集
2021/05/10 05:20
2021/05/10 20:31
2021/05/10 23:22 編集