#python #python_3x
def _create_candle(self, index):
candle = Candle(index)
self.candles.append(candle)
def o(response):
candle.open = response.result
def h(response):
candle.high = response.result
def l(response):
candle.low = response.result
def c(response):
candle.close = response.result
def v(response):
candle.volume = response.result
def t(response):
candle.time = response.result
self._api.send("O", lambda response: candle.open = response.result, self._ds_id,
index) # не работает
self._api.send("H", h, self._ds_id, index)
self._api.send("L", l, self._ds_id, index)
self._api.send("C", c, self._ds_id, index)
self._api.send("V", v, self._ds_id, index)
self._api.send("T", t, self._ds_id, index)
Ответы
Ответ 1
В лябмдах нельзя присваивать элементы. Попытки присвоить приведут к ошибке: SyntaxError: can't assign to lambda. Но можно попытаться обойти ограничение, пример: class Foo: pass f = Foo() f.a = 10 print(f.a) # 10 # result = lambda x: f.a = x # Ошибка result = lambda x: setattr(f, 'a', x) result(78) print(f.a) # 78 result('abc') print(f.a) # abcОтвет 2
Вы уже в будущем - в Питон 3.8 это будет возможно, хотя с маленькой разницей - := вместо = и с добавленными скобками: self._api.send("O", lambda response: (candle.open := response.result), self._ds_id, index) # будет работать Но затем в lambda функциях ожидается в качестве значения только выражение, а не команда присваивания. (Вы вероятно знаете C/C++, где это возможно а где присваивание само считается выражением.)
Комментариев нет:
Отправить комментарий