if does not evaluate

J

Jim Newton

A question that has bothered me ever since looking at python
the first time is that not everything evaluates to something.
I am wondering what is the reason for this. Would making
everying evaluate have caused the langugage to be less
efficient execution-wise? or was it a choice to enforce some
sort of standard?

I've read a few discussions about the fact that there is
no if/then/else operartor.

Wouldn't this problem be easily solved by making the built
in keywords actually be evaluatable.

I.e., x = if something:
expr1
else:
expr2

parentheses would of course be optional as they are for
all expressions.
 
B

bruno modulix

Jim Newton a écrit :
A question that has bothered me ever since looking at python
the first time is that not everything evaluates to something.
I am wondering what is the reason for this. Would making
everying evaluate have caused the langugage to be less
efficient execution-wise? or was it a choice to enforce some
sort of standard?

I've read a few discussions about the fact that there is
no if/then/else operartor.

Wouldn't this problem be easily solved by making the built
in keywords actually be evaluatable.

I.e., x = if something:
expr1
else:
expr2

parentheses would of course be optional as they are for
all expressions.
Python is, by design, an 'instruction-based' language, not an
'expression-based' language. Search this ng for discussions on the
inclusion of ternary operator if you want to learn more about this point...

Now if you're looking for a language pretty similar to Python
(very-hi-level, highly dynamic, interpreted, clean and readable syntax,
friendly and enthusiast communauty etc) where everything's an
expression, you may want to try Ruby. It's not as easy to learn, it as
its own warts too, and it has not yet reached such a 'critical mass' as
Python in terms of libs and communauty, but it's a really nice and
usable language too.

Bruno (who likes both languages...)
 
T

Tor Iver Wilhelmsen

Jim Newton said:
I.e., x = if something:
expr1
else:
expr2

Try exploiting that a boolean expression evaluates to 0 or 1:

x = (expr1, expr2)[something];

Keep in mind that this might break on some later release of Python if
they decide to make boolean its own type, but at least it works for
2.3.3.
 
M

Matteo Dell'Amico

Tor said:
x = (expr1, expr2)[something];

Keep in mind that this might break on some later release of Python if
they decide to make boolean its own type, but at least it works for
2.3.3.

I think (and I hope) it will never be so, since bool is already its own
type and, as the documentation states,

"In numeric contexts (for example when used as the argument to an
arithmetic operator), they behave like the integers 0 and 1, respectively."

So, even if this a little "perlish" :), I think it will continue to
work in future versions of python, since otherwise it would break too
much existing code.
 
?

=?ISO-8859-1?Q?Holger_T=FCrk?=

Tor said:
I.e., x = if something:
expr1
else:
expr2


Try exploiting that a boolean expression evaluates to 0 or 1:

x = (expr1, expr2)[something];

This is eagerly evaluated. If only one expression is meant to
be evaluated, the whole thing should look like this:

x = (lambda: expr1, lambda: expr2)[bool(something)]()

I added bool() to allow tests returning other values than
0 or 1.

Greetings,

Holger
 
T

Tor Iver Wilhelmsen

Matteo Dell'Amico said:
So, even if this a little "perlish" :), I think it will continue to
work in future versions of python, since otherwise it would break too
much existing code.

Yes, and at least one would hope it would work anyway with a cast,
that is "int(something)".
 
J

Jim Newton

this suggestion does not work because
expr2 evaluates even if expr1 is TRUE.

I.e., x = if something:
expr1
else:
expr2


Try exploiting that a boolean expression evaluates to 0 or 1:

x = (expr1, expr2)[something];

Keep in mind that this might break on some later release of Python if
they decide to make boolean its own type, but at least it works for
2.3.3.
 
T

Tor Iver Wilhelmsen

Jim Newton said:
this suggestion does not work because
expr2 evaluates even if expr1 is TRUE.

Yes, someone pointed that out already :) - the solution was to use
lambda expressions, which is reasonable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top