Магический метод, &

Подскажите, как в данном случае перезаписать применение оператора & в Python через магический метод __iand__?

def __iand__(self, others):
    self & others = search_general(self, others.id)

Ответы (2 шт):

Автор решения: Danis

если вы ходите чтобы при a&b вернулось search_general(a, b.id) то так:

def __and__(self, others):
    return search_general(self, others.id)

если search_general это метод класса то так:

def __and__(self, others):
    return self.search_general(others.id) 
→ Ссылка
Автор решения: Klim

Объявляете метод __and__ реализуете как вам будет угодно реакция на &. И возвращаете результат через return ...

class A(object):
  def __and__(self, other):
      return 'New format: {} & {}'.format(self, other)

a = A()
a2 = A()

print(a & a2)
>>> New format: <__main__.A object at 0x7f16fe8b3970> & <__main__.A object at 0x7f16fe8ab4f0>
→ Ссылка