Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
Where is the logical 'or' equivalent in the 'operator' module?

Adding or multiplying a large list of numbers in Python can elegantly be done by folding the list with the addition or multiplication operator:

import functools, operator
lst = range(1,100)
sum  = functools.reduce(operator.add, lst, 0)
prod = functools.reduce(operator.mul, lst, 1)    

This needs the function equivalents of the operators + and * which are provided by the operator module as operator.add and operator.mul, respectively.

If I want to use the same idiom with the operator or:

ingredients       = ['onion', 'celery', 'cyanide', 'chicken stock']
soup_is_poisonous = functools.reduce(operator.or, map(is_poisonous, ingredients), False)

... then I discover that operator doesn't have a function equivalent of the logical and and or operators (though it has one for logical not)

Of course, I can trivially write one that works:

def operator_or(x,y):
  return x or y

But I wonder: why are there no operator.or and operator.and in operator? Bitwise and and or are there, but not the logical ones.

Of course this is just a minor annoyance, and the answer may well be the same as with the missing identity function: that it is easy to write one. But this holds for * and + as well, so why the difference?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

all is short-circuiting logical-and.
any is short-circuiting logical-or.

No need to put versions that take exactly two arguments (instead of an iterable) into the operator module, I guess.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda