6

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?

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • 8
    Because it's **not** an operator. Assignment is a _statement_: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements. Depending on version you may be able to use an _assignment expression_: https://docs.python.org/3/reference/expressions.html#assignment-expressions. – jonrsharpe Oct 10 '21 at 11:18
  • 1
    @jonrsharpe Oh, the google search on "assignment operator in Python" gave so many results (e.g. https://www.geeksforgeeks.org/assignment-operators-in-python/)! – AlwaysLearning Oct 10 '21 at 11:21
  • 2
    People may refer to `=` as an operator, but strictly it is not, it's just part of the assignment statement syntax. – jonrsharpe Oct 10 '21 at 11:22
  • @jonrsharpe Coming from C++ background, I expected = to be an operator. I also expected people to understand the difference and not call something an operator unless it really is... – AlwaysLearning Oct 10 '21 at 11:26
  • Nothing identified on that page is actually an operator, they're all delimiters in assignment statements. Augmented assignment expressions, which would have operators, were explicitly ruled out. There's not much we can do about your expectations, but the official docs might be a better source. – jonrsharpe Oct 10 '21 at 17:47
  • 7
    This question is being discussed on [meta](https://meta.stackoverflow.com/questions/412173). – cigien Oct 11 '21 at 16:41

1 Answers1

6

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>
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Mohammad Jafari
  • 1,742
  • 13
  • 17
  • Yes, I know the difference. Coming from C++ background, I expected `=` to be an operator. Why do you need two different notations? – AlwaysLearning Oct 10 '21 at 11:23
  • 4
    @AlwaysLearning This was not an intended feature in Python initially and the reason to do so was probably to disallow writing codes like so as it tends to make the code look less readable. Even the `:=` is not encouraged enough in the Python community – Melvin Abraham Oct 10 '21 at 11:26