In Python, what is the difference between expressions and statements?
-
3Due to python's definition that expressions are a subset of statements, the question can be revised as: which statements are not expressions? – Youjun Hu Apr 28 '22 at 07:51
17 Answers
Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator ()
the subscription operator []
and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:
3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7
Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:
# all the above expressions
print 42
if x: do_y()
return
a = 7

- 4,724
- 5
- 35
- 49

- 574,206
- 118
- 941
- 841
-
32
-
69@bismigalis: Every valid Python expression can be used as a statement (called an ["expression statement"](http://docs.python.org/2/reference/simple_stmts.html#expression-statements)). In this sense, expressions *are* statements. – Sven Marnach Nov 25 '13 at 18:05
-
2Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear. – Jim Dennis Feb 08 '15 at 23:26
-
@JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this. – Sven Marnach Feb 09 '15 at 22:02
-
`if condition: do_y()` is a statement, but the ternary operator `x if condition else y` is an expression (conditional expression) – nonybrighto Sep 25 '18 at 15:45
-
@SvenMarnach I mean even though we have this `expression_stmt ::= expression_list` I don't think it's all the same to just use them interchangeably. I mean I see where you're coming from, but the above EBNF rule pretty much produces anything you can have on the right-hand side of an `assignment statement`. Outside the *REPL*, you wouldn't be able to write a useful program just using **expressions** (values). You have to keep references to them, pass them around, etc. All those are **statements**, where a **statement** is basically just another word for a **declaration**. – Marius Mucenicu May 27 '19 at 19:17
-
@George I'm not completely sure what your point is. I never said that the terms _statement_ and _expression_ can be used interchangeably; just that every expression can also be a statement. – Sven Marnach May 27 '19 at 21:26
-
@SvenMarnach heh that's fair tbh, I don't know why I made the assumption that's what you meant, sry. I was also looking to enforce the notions that's why I landed here. Indeed expressions can be statements, which is why we have `expression statements`. Those are particularly useful when using the REPL, and not very useful outside, as you'd want to use `simple` or `compound` statements when you build programs that will be fed to python at a later time. – Marius Mucenicu May 28 '19 at 08:59
-
2@George Fair enough. :) Expression statements are quite useful even outside of the REPL – it's quite common to use function call expressions as expression statements, e.g. `print("Hello world!")` or `my_list.append(42)`. – Sven Marnach May 28 '19 at 12:09
-
@SvenMarnach I do not think you can consider print(“hello world”) an expression statement because an expression is anything that returns a value but print() is a void function which is simply not an expression. Void function calls are statements, but not expression statements. Or maybe I just misunderstand something? The only practical use of expression statements I can imagine is a function which has an important side effect like void functions, but they return a value in which you are not interested. – Patrik Nusszer Jul 18 '19 at 10:12
-
1@PatrikNusszer In Python, _every_ function call returns a value. There are no "void" functions. If you don't return a value explicitly, `None` is returned instead, and this is also what `print()` returns (at least in Python 3). Try e.g. `print(print())` or `a = print()` to see that a call to `print()` is indeed an expression. – Sven Marnach Jul 20 '19 at 12:36
-
1@SvenMarnach I'm struggling to understand some of your examples. Isn't `yield 7` a statement as it is not made up of only identifiers, literals and operators? – Will Taylor Aug 22 '19 at 11:08
-
7@WillTaylor Everything that yields a value is an expression, i.e. everything you could write on the write-hand side of an assignment. Since `a = yield 7` is valid, `yield 7` is an expression. A long time ago, `yield` was introduced as a statement, but it was generalized to an expression in [PEP 342](https://www.python.org/dev/peps/pep-0342/). – Sven Marnach Aug 22 '19 at 19:37
-
What is actually the real impact or consequence of the difference of these two terms in practice? Beyond the theory - aren't these terms often used interchangeably in practice(?) – Wlad Dec 15 '19 at 13:24
-
@Wlad I don't often see them used interchangeably. The terms often occur in descriptions of programming language syntax; e.g. the arguments of a Python funciton call are expressions. If you use a statement there that isn't an expression, the code won't compile (and it doesn't make sense anyway). Of course there is some overlap, since any expression can also be used as a statement. – Sven Marnach Dec 16 '19 at 08:12
-
@SvenMarnach What do you mean "just that every expression can also be a statement"? If I type `2+3` in a REPL would it be considered a statement? Furthermore, `_` return the value of the last evaluated expression in REPL. If I type `2+3; print(); _` I get `5`. If `print()` is an expression why I am not getting `None`? – ado sar Feb 28 '23 at 08:34
-
@adosar The REPL treats "expression statements", i.e. statements that consist only of an expression, in a special way. As part of a program, the value an expression statement evaluates to is simply discarded. In the REPL, however, it is printed and stored in the `_` special variable as long as it is not `None`. You can easily verify this with the expressions `2` and `None`. The former will be printed back to you and stored in `_`, the latter won't. This makes the REPL more convenient to use, but it's kind of unrelated to the definition of statements and expressions. – Sven Marnach Feb 28 '23 at 09:40
-
@SvenMarnach Thanks for your feedback! With regards to every expression can be a statement: a). `2 + 3` is an expression statement and b). `a = 2 + 3` is also a statement. As you said every expression can be a statement, which means that in case b). `2 + 3` *is part of a statement* while in case a). *is the statement itself*? – ado sar Feb 28 '23 at 15:01
-
@adosar Yes, you can inlcude `2+3` as a statement in a Python program. It doesn't do anything, since the value is discarded, but it's valid code. Generally only statements with side effects are useful. – Sven Marnach Mar 01 '23 at 09:44
Expression -- from the New Oxford American Dictionary:
expression: Mathematics a collection of symbols that jointly express a quantity : the expression for the circumference of a circle is 2πr.
In gross general terms: Expressions produce at least one value.
In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.
Python expressions from Wikipedia
Examples of expressions:
Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:
>>> 23
23
>>> 23l
23L
>>> range(4)
[0, 1, 2, 3]
>>> 2L*bin(2)
'0b100b10'
>>> def func(a): # Statement, just part of the example...
... return a*a # Statement...
...
>>> func(3)*4
36
>>> func(5) is func(a=5)
True
Statement from Wikipedia:
In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements. A statement will have internal components (e.g., expressions).
Python statements from Wikipedia
In gross general terms: Statements Do Something and are often composed of expressions (or other statements)
The Python Language Reference covers Simple Statements and Compound Statements extensively.
The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:
- List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.
- The
if
is usually a statement, such asif x<0: x=0
but you can also have a conditional expression likex=0 if x<0 else 1
that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1;
- You can write you own Expressions by writing a function.
def func(a): return a*a
is an expression when used but made up of statements when defined. - An expression that returns
None
is a procedure in Python:def proc(): pass
Syntactically, you can useproc()
as an expression, but that is probably a bug... - Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have
func(x=2);
Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The assignment statement ofx=2
inside of the function call offunc(x=2)
in Python sets the named argumenta
to 2 only in the call tofunc
and is more limited than the C example.

- 98,345
- 23
- 131
- 206
-
3"From my Dictionary" meaning your personal opinion or the dictionary you own like the oxford dictionary ? Thanks – Talespin_Kit Oct 05 '19 at 05:13
-
3@Talespin_Kit: *...your personal opinion or the dictionary you own like the Oxford dictionary?* Good question. I used the Apple Dictionary app on a Mac which is based on New Oxford American Dictionary. – dawg Jan 14 '20 at 17:30
-
@dawg: A couple of the examples you've mentioned, for "blurriness", seem questionable to me. Just because a list comprehension uses the keyword `for`, can we really say that a list comprehension contains a `for` statement ? I don't think so. Similarly, just because a conditional expression uses the keyword `if`, I don't think that we can say that a conditional expression contains an `if` statement and is therefore "blurring" the distinction between statement and expression. – fountainhead Apr 05 '23 at 02:37
Though this isn't related to Python:
An expression
evaluates to a value.
A statement
does something.
>>> x + 2 # an expression
>>> x = 1 # a statement
>>> y = x + 1 # a statement
>>> print y # a statement (in 2.x)
2

- 2,259
- 1
- 19
- 29

- 126,773
- 69
- 172
- 181
-
4But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well. – Jan 18 '11 at 19:32
-
-
@A A: Easy example: `sys.stdout.write('see?\n')` (easier in 3.x where print is a function and can thus be called as part of an expression). Unless of course you have a very special definition of "does something". – Jan 18 '11 at 19:34
-
5
-
14y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error. – Arglanir Feb 04 '13 at 09:52
-
@user225312, you said a `statement` does something. So what does a null statement do? – Pacerier Oct 22 '13 at 06:06
-
5
-
2@SteveFreed Aren't statements made of expressions? If so, saying *expression statement* is rather redundant. – Nameless Apr 06 '21 at 01:14
-
-
y = x + 1 is a statement?? I read "https://www.scaler.com/topics/expression-in-python/", that its an expression! – Nomi Dec 20 '22 at 13:26
-
A statement doesn't necessarily do anything. `pass` is a statement that simply fulfills a syntactic requirement, without affecting program state in anyway. (There's no such thing as a null statement in Python, which is why `pass` is required.) – chepner Mar 06 '23 at 14:22
-
@Nomi That was written by someone who apparently doesn't know what they are talking about. In languages like C, an assignment is an expression whose value is the value being assigned, which is what lets you chain assignments. `x = y = 3` would asking the value of `y = 3` (3) to the variable `x`. In Python `x = y = 3` is a single statement with the semantics of assigning the value 3 first to `x`, then to `y`. `x = x + 10` *contains* an expression, but is not itself an expression. – chepner Mar 06 '23 at 14:24
-
1@Nameless Statements serve a specific syntactic purpose. The point of an expression statement is to allow the *parser* to accept a single expression where it expects a statement. Assignment statements are recognized by their use of an `=` outside of a function call. Every other statement includes a special keyword that cannot be used in an expression. (The exception is the `yield` statement, which shares the `yield` keyword with yield expressions, which were added to the language *after* yield statements. It's a bit more of a mess than can be comfortably explained here.) – chepner Mar 06 '23 at 14:33
-
@chepner, "Assignment statements are recognized by their use of an = outside of a function call'. Why only *outside* of a function call ? What usage were you trying to exclude ? Were you trying to exclude the use of = in the keyword args of a function call ? – fountainhead Apr 05 '23 at 02:17
-
Good question; if keyword aruguments *aren't* what I was thinking of, I don't know what it might have been :) – chepner Apr 05 '23 at 12:45
An expression is something that can be reduced to a value.
Let's take the following examples and figure out what is what:
"1+3"
and "foo = 1+3"
.
It's easy to check:
print(foo = 1+3)
If it doesn't work, it's a statement, if it does, it's an expression. Try it out!
Another statement could be:
class Foo(Bar): pass
as it cannot be reduced to a value.

- 13,566
- 13
- 80
- 126
-
1As executing your first example would show, assignment is *not* an expression (not really, that is - `a = b = expr` is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is. – Jan 18 '11 at 19:26
-
`class Foo(bar):` is the beginning of a statement, not a complete statement. – Sven Marnach Jan 18 '11 at 19:28
-
1`foo = 1+3` is NOT an expression. It is a statement (an assignment to be precise). The part `1+3` is an expression though. – Pithikos Apr 17 '15 at 13:25
-
5My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer. – Flavius Jan 31 '17 at 07:32
-
-
1The print does not work for the above case because it considers `foo=1+3` as a keyword argument, which is not expected by the print function. – Youjun Hu Apr 28 '22 at 07:58
-
1`print(end="1+3")` works, but `end="1+3"` is neither an expression nor a statement (though it *looks* like a statement, it's just part of the syntax of a function call. – chepner Mar 06 '23 at 14:19
Statements represent an action or command e.g print statements, assignment statements.
print 'hello', x = 1
Expression is a combination of variables, operations and values that yields a result value.
5 * 5 # yields 25
Lastly, expression statements
print 5*5

- 5,625
- 2
- 38
- 39
-
This is an outdated example, as `print` is an ordinary function now, not a statement. – chepner Mar 06 '23 at 14:37
- An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.
- Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.

- 27,002
- 5
- 88
- 78
An expression is something, while a statement does something.
An expression is a statement as well, but it must have a return.
>>> 2 * 2 #expression
>>> print(2 * 2) #statement
PS:The interpreter always prints out the values of all expressions.

- 1,458
- 11
- 26

- 171
- 1
- 6
-
1
-
@Adalcar No, there is a distinction between the two as you've probably come to understand from reading all the answers here. An expression tells the interpreter that something needs to be evaluated, calculated, reduced, etc. for example: >>>>5 + 5. A statement does not. Think of the statement as the block of code, that instructs the interpreter to do something (besides evaluation). So as a simple example, x = 5 + 5. This is an assignment STATEMENT, which binds a name to an object in Python. Now, before the result is bound to x, the 5 + 5 EXPRESSION is evaluated. – mrwonderfulness Jun 10 '22 at 02:28
-
@Adalcar btw, even 5 + 5 alone in Python is a type of statement, know as an "expression statement", but is is not useful outside of the REPL. You could look at a statement as a structural block or step of code in the program (storing values, defining control flow, etc.), but expressions (which are always housed within some kind of statement) as the grunt work done by the cpu. Statements manage and direct how, when, where, etc. expressions are executed. Statements are like a foreman instructing the work, the expression is the guy digging the ditch. :) – mrwonderfulness Jun 10 '22 at 02:41
-
fyi: Python "expression statements": https://docs.python.org/3/reference/simple_stmts.html#expression-statements – mrwonderfulness Jun 10 '22 at 02:43
STATEMENT:
A Statement is a action or a command that does something. Ex: If-Else,Loops..etc
val a: Int = 5
If(a>5) print("Hey!") else print("Hi!")
EXPRESSION:
A Expression is a combination of values, operators and literals which yields something.
val a: Int = 5 + 5 #yields 10

- 143
- 3
- 12
-
1This is a duplicate of this existing answer: https://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python/4728162#4728162. – karel Mar 09 '19 at 15:58
-
1Maybe it's duplicate but it shares my views for the question above. No offence – Raja Shekar Mar 17 '19 at 07:52
-
`pass` is a statement. It is neither an action nor does it do anything. `5 + 5` is an expression; `a = 5 + 5` is not. (`val` is just a syntax error in both cases.) – chepner Mar 06 '23 at 14:38
Expressions always evaluate to a value, statements don't.
e.g.
variable declaration and assignment are statements because they do not return a value
const list = [1,2,3];
Here we have two operands - a variable 'sum' on the left and an expression on the right. The whole thing is a statement, but the bit on the right is an expression as that piece of code returns a value.
const sum = list.reduce((a, b)=> a+ b, 0);
Function calls, arithmetic and boolean operations are good examples of expressions.
Expressions are often part of a statement.
The distinction between the two is often required to indicate whether we require a pice of code to return a value.

- 61
- 1
- 3
References
Expressions and statements
2.3 Expressions and statements - thinkpython2 by Allen B. Downey
An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions:
>>> 42
42
>>> n
17
>>> n + 25
42
When you type an expression at the prompt, the interpreter evaluates it, which means that it finds the value of the expression. In this example, n has the value 17 and n + 25 has the value 42.
A statement is a unit of code that has an effect, like creating a variable or displaying a value.
>>> n = 17
>>> print(n)
The first line is an assignment statement that gives a value to n. The second line is a print statement that displays the value of n. When you type a statement, the interpreter executes it, which means that it does whatever the statement says. In general, statements don’t have values.

- 780
- 1
- 11
- 15
An expression translates to a value.
A statement consumes a value* to produce a result**.
*That includes an empty value, like: print()
or pop()
.
**This result can be any action that changes something; e.g. changes the memory ( x = 1) or changes something on the screen ( print("x") ).
A few notes:
- Since a statement can return a result, it can be part of an expression.
- An expression can be part of another expression.

- 2,377
- 27
- 32
-
`pass` is a statement: it does not consume a value or produce a result. – chepner Mar 06 '23 at 14:41
-
@chepner, it surely produces a result, it increases program counter by 1. – Pontios Mar 07 '23 at 20:24
-
That's not an observable property of your program, only an implementation detail of your Python interpreter. – chepner Mar 07 '23 at 20:25
-
-
You didn't, *I* did. Incrementing a program counter by 1 is something that happens inside the *interpreter*, not your own program being executed *by* the interpreter. – chepner Apr 06 '23 at 12:40
Statements before could change the state of our Python program: create or update variables, define function, etc.
And expressions just return some value can't change the global state or local state in a function.
But now we got :=
, it's an alien!

- 527
- 5
- 14
-
Expressions could always change state: every function call is an expression. `:=` just allows names to be bound in value outside of a function, which previously was the only way to wrap an assignment into an expression. – chepner Mar 06 '23 at 14:41
Expressions:
- Expressions are formed by combining
objects
andoperators
. - An expression has a value, which has a type.
- Syntax for a simple expression:
<object><operator><object>
2.0 + 3
is an expression which evaluates to 5.0
and has a type float
associated with it.
Statements
Statements are composed of expression(s). It can span multiple lines.

- 196
- 1
- 12
A statement contains a keyword.
An expression does not contain a keyword.
print "hello"
is statement, because print
is a keyword.
"hello"
is an expression, but list compression is against this.
The following is an expression statement, and it is true without list comprehension:
(x*2 for x in range(10))

- 24,690
- 13
- 50
- 55

- 27
- 1
-
4That strongly depends on your definition of a 'keyword'. `x = 1` is a perfectly fine statement, but does not contain keywords. – Joost May 08 '14 at 20:56
-
No, e.g. `is` is a keyword but `x is y` is not necessarily a statement (in general it is just an expression). – benjimin Feb 13 '20 at 02:07
Expressions are evaluated to produce a value, whereas statements are executed to perform an action or task.

- 11
- 2
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 14 '23 at 05:23
Expressions and statements are both syntactic constructs in Python's grammar.
An expression can be evaluated to produce a value.
A statement is a standalone piece of a Python program (which is simply a sequence of 0 or more statements, executed sequentially). Each kind of statement provides a different set of semantics for defining what the program will do.
Statements are often (but not always) constructed from expressions, using the values of the expressions in their own unique way. Usually, there is some keyword or symbol that can be used to recognize each particular kind of statement.
An exhaustive (as of Python 3.11) list of the various kinds of statements.
- An expression statement evaluates an expression and discards the result. (Any expression can be used; there are no other distinguishing features of an expression statement.)
- An assignment statement evaluates an expression and assigns its value to one or more targets. It is identified by the use of
=
outside of the context of a function call. - An assert statement evaluates an expression and raises an exception if the expression's value is false. It is identified by the
assert
keyword. - A pass statement does nothing: it's job is to provide the parser with a statement where one is expected but no other statement is appropriate. It is identified by (and consists solely of) the
pass
keyword. - A del statement removes a binding from the current scope. No expression is evaluated; the binding must be a name, an indexed name, or a dotted name. It is identified by the
del
keyword. - A return statement returns control (and the value of an optional expression, defaulting to
None
) to the caller of a function. It is identified by thereturn
keyword. - A yield statement returns the value of an optional expression (defaulting to
None
) to the consumer of a generator. It is identified by theyield
keyword. (yield
is also used in yield expressions; context will make it clear when a statement or expression is meant.) - A raise statement evaluates an expression to produce an exception and circumvents the normal execution order. It is identified by the
raise
keyword. - A break statement terminates the currently active loop. It is identified (and consists solely of) the
break
keyword. - A continue statement skips the rest of the body of the currently active loop an attempts to start a new iteration. It is identified (and consists solely of) the
continue
keyword. - An import statement potentially defines a new
module
object and binds one or more names in the current scope to either amodule
or values defined in the module. (Like theclass
statement, there are various hooks available to override exactly what happens during the execution of an import statement.) There are several forms, all of which share theimport
keyword. - A global and nonlocal statement alters the scope in which an assignment to a particular name operates. They are identified by the
global
andnonlocal
keywords, respectively. - An if statement evaluates a boolean expression to select a set of statements to execute next. It is identified by the
if
keyword. - A while statement evaluates a boolean expression to determine whether to execute its body, repeating the process until the expression become false. It is identified by the
while
keyword`. - A for loop evaluates an expression to produce an iterator, which it uses to perform some name bindings and repeatedly execute a set of statements with those bindings. It is identified by the
for
keyword (which is also shared by generator expressions and comprehensions, though context makes it clear which is which). - A try statement executes a set of statements and catches any exceptions those statements might raise. It is identified by the
try
keyword. - A with statement evaluates an expression to produce a context manager, which provides a pair of functions to call before and after a set of statements is executed. It is identified by the
with
keyword. - A function definition produces a callable object that wraps one or more statements to be execute when the object is called. It is identified by the
def
keyword. - A class statement evaluates a set of statements to produce a set of bindings to be used as attributes of a new type. (Like an import statement, there are various hooks to override exactly what happens during the execution of a class statement.) It is identified by the
class
keyword. - Coroutines are produced by various function, for, and with statements using the
async
keyword to distinguish them from their synchronous counterparts.

- 497,756
- 71
- 530
- 681
Python calls expressions "expression statements", so the question is perhaps not fully formed.
A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#
An expression statement is limited to calling functions (e.g., math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.

- 16,451
- 4
- 26
- 27
-
11No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements". – Sven Marnach Jan 18 '11 at 19:37
-
-
@Sven Marnach _No, Python doesn't call expressions "expression statements"._ — Hey, but as per the EBNF rule, isn't every expression an "expression statement"? I am not sure what's wrong with calling expressions "expression statements". – Niraj Raut May 01 '21 at 07:41
-
@NirajRaut As an example, in the assignment statement `a = 42` the right-hand side `42` is an expression, but it's not an expression statement. Any expression could be used as a statement, but not every expression is actually used as a statement. – Sven Marnach May 03 '21 at 12:49
-
@Sven Marnach Off-Topic: I have a question regarding `__init__` and `__new__`. Just want to ask if a term like "constructor" exists in the Python language. I have seen that the docs use it somewhere, but I haven't seen where the term is explicitly defined. Is "constructor" part of the Python language? Does `__init__` and `__new__` together somehow from the constructor? You, being the pedantic guy, I would like to know your opinion on this. Also, thanks for the clarification. – Niraj Raut May 04 '21 at 11:28
-
@NirajRaut Technically, `__new__()` is the constructor, since it returns a new instance of the class, and `__init__()` is an initializer function. However, most people call `__init__()` the constructor, since it's a lot more commonly used, and field initialization is performed in the constructor in many other languages. I don't think [there's an official definition of what constitutes the constructor in Python](https://www.google.com/search?q=inurl%3Ahttps%3A%2F%2Fdocs.python.org%2F3%2Freference+%22constructor%22). – Sven Marnach May 04 '21 at 14:26
-
@Sven Marnach This [link](https://docs.python.org/3/reference/datamodel.html#object.__new__) uses the term "constructor expression" again and again. – Niraj Raut May 04 '21 at 19:51
-
@NirajRaut You are right. It uses it for an expression calling a class object, which first calls `__new__()` and then calls `__init__()` on the return value of `__new__()`. Neither of these two special methods is called the constructor in the docs. – Sven Marnach May 05 '21 at 14:21
-
@Sven Marnach: From the same link: "...where self is the new instance and the remaining arguments are the same as were passed to the *object constructor.*" What about this? – Niraj Raut May 06 '21 at 06:02
-
@NirajRaut I interpret that the same way. The documentation seems to call expressions of the form `MyClass(*args)` constructor expressions. The arguments are first passed to `__new__()`, then to `__init__()`. – Sven Marnach May 07 '21 at 08:42
-
@Sven Marnach Is ```__new__()``` the object constructor? Because the constructor expression of the form ```MyClass(*args)``` passes the arguments to ```__new__()``` first. – Niraj Raut May 07 '21 at 09:02
-
@NirajRaut There is no clear answer, as I said before. I can't see any evidence that the documentation uses the term this way, and it doesn't seem to be common to use the term that way in the Python community. If you want to make sure that people understand you correctly, just use `__new__()` and `__init__()` explicitly. – Sven Marnach May 07 '21 at 09:08
-
An expression is only an expression statement when it appears where a statement is expected. In `x = 2*3 + 5*6`, neither `2*3` nor `5*6` is an expression statement; they are just expressions. In the expression statement `print("hello, world")`, the statement consists of a single function-call expression. – chepner Mar 06 '23 at 14:35