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

255
Visualizações
Using assignment as operator

Consider:

course_db = Course(title='Databases')
course_db.save()

Coming from a C++ background, I would expect (course_db = Course(title='Databases')) to behave like it would in C++, that is, assign Course(title='Databases') to course_db and return the assigned object so that I can use it as part of a larger expression. For example, I would expect the following code to do the same thing as the code above:

(course_db = Course(title='Databases')).save()

This assumption got support from some quick Google searches using terms like "assignment operator in Python", e.g. this article. But when I tried this, I got a syntax error.

Why can't I do this in Python, and what can I do instead?

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

0

You should do some more research about the differences between statements and expressions in Python.

If you are using Python 3.8+, you can use the := operator:

In [1]: class A:
   ...:     def save(self):
   ...:         return 1
   ...: 

In [2]: (a := A()).save()
Out[2]: 1

In [3]: a
Out[3]: <__main__.A at 0x7f074e2ddaf0>
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