前者が正解です。
Python
1class MyInt:
2 def __init__(self, num):
3 self._num = num
4
5 def __add__(self, other):
6 print('add!')
7 return MyInt(self._num + other._num)
8
9 def __str__(self):
10 return str(self._num)
11
12
13n1 = MyInt(1)
14n2 = MyInt(2)
15
16print('---')
17a = n1 + n2
18print('---')
19
20print(a)
実行結果 Wandbox
工夫次第で評価を遅延させることもできますが、処理を追いづらくなるのでお勧めしません。
Python
1a = 1
2b = 2
3
4f = lambda: a + b
5print(f())
6
7b = 3
8print(f())
実行結果 Wandbox
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/23 08:33