[Python-ideas] Statements vs Expressions... why?

Cliff Wells cliff at develix.com
Sun Sep 14 23:41:19 CEST 2008


On Sun, 2008-09-14 at 23:34 +0200, Mathias Panzenböck wrote:
> If I understand this right, then this would become legal too:
> 
> x = if cond(a):
> 	return b

return would always be a syntax error outside of a function.

> 
> So what value is assigned to x when cond(a) is True, what value when it is
> False? 

x = if False: 1 # evaluates to None
x = if True: 1 # evalutates to True
x = if True: return 1 # syntax error: return outside of function

def foo():
    x = if True: return 1 # exits the enclosing function. value of
if-expression is moot as it's never reached.


> What value does return return? The thing is that return does definitively
> not return anything, but as a statement, it can be placed everywhere in a block.

Not that it matters, but return always returns something, either a
user-specified value or None.

Regards,
Cliff





More information about the Python-ideas mailing list