Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a function call an expression in python?

According to this answer, a function call is a statement, but in the course I'm following in Coursera they say a function call is an expression.

So this is my guess: a function call does something, that's why it's a statement, but after it's called it evaluates and passes a value, which makes it also an expression.

Is a function call an expression?

like image 581
Eric Liddel Avatar asked May 21 '13 00:05

Eric Liddel


People also ask

Is a function call an expression?

A function call is an expression that includes the name of the function being called or the value of a function pointer and, optionally, the arguments being passed to the function.

What is function call in Python?

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

What are the expressions in Python?

In Python, operators are special symbols that designate that some sort of computation should be performed. The values that an operator acts on are called operands. A sequence of operands and operators, like a + b - 5 , is called an expression. Python supports many operators for combining data objects into expressions.

What is a function call?

A function call is an expression that passes control and arguments (if any) to a function and has the form: expression (expression-listopt) where expression is a function name or evaluates to a function address and expression-list is a list of expressions (separated by commas).


1 Answers

A call is an expression; it is listed in the Expressions reference documentation.

If it was a statement, you could not use it as part of an expression; statements can contain expressions, but not the other way around.

As an example, return expression is a statement; it uses expressions to determine it's behaviour; the result of the expression is what the current function returns. You can use a call as part of that expression:

return some_function()

You cannot, however, use return as part of a call:

some_function(return)

That would be a syntax error.

It is the return that 'does something'; it ends the function and returns the result of the expression. It's not the expression itself that makes the function return.

If a python call was not an expression, you'd never be able to mix calls and other expression atoms into more complex expressions.

like image 160
Martijn Pieters Avatar answered Oct 06 '22 16:10

Martijn Pieters