473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

if does not evaluate

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.

Jul 18 '05 #1
8 1240
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...)
Jul 18 '05 #2
Jim Newton <ji***@rdrop.com> writes:
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.
Jul 18 '05 #3
Tor Iver Wilhelmsen <to*****************@broadpark.no> writes:
x = (expr1, expr2)[something];


Um, other way around of course :) :

x = (expr2, expr1)[something];
Jul 18 '05 #4
Tor Iver Wilhelmsen wrote:
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.

--
Ciao,
Matteo
Jul 18 '05 #5


Tor Iver Wilhelmsen wrote:
Jim Newton <ji***@rdrop.com> writes:

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

Jul 18 '05 #6
Matteo Dell'Amico <de***@toglimi.linux.it> writes:
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)".
Jul 18 '05 #7
this suggestion does not work because
expr2 evaluates even if expr1 is TRUE.
Tor Iver Wilhelmsen wrote:
Jim Newton <ji***@rdrop.com> writes:

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.


Jul 18 '05 #8
Jim Newton <ji***@rdrop.com> writes:
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.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Spiegel | last post by:
Hi all, Is it possible to have an uncompiled C# expression evaluated at runtime? I'd like to store an expression within an XML file then evaluate it when the time comes, something like: ...
19
by: Siemel Naran | last post by:
Does true ^ true return false? int silly() { return 3; } int main() { bool t1 = silly(); bool t2 = true; cout << (t1 ^ t2) << '\n'; }
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
23
by: centurian | last post by:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++ 6" and it ran without any errors. Does this thing make any sense? Is it of some use or some problem with grammar/CFG of...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
16
by: nephish | last post by:
Hey there, i have been learning python for the past few months, but i can seem to get what exactly a lamda is for. What would i use a lamda for that i could not or would not use a def for ? Is...
2
by: Björn Langhof | last post by:
Hello. I want to evaluate a XPath-Expression only on a subtree of the whole xml-document. 1. I select a node of the XML-document 2. Then a want to select specific nodes below the node chosen...
1
by: shellon | last post by:
Hi all: when I use XPather(a firefox extension) to evaluate the expression: "/html/body/table/tbody/tr/td/table/tbody/tr/td/div/ul/li" it tells me there are 7 matching Nodes. but when I use...
15
by: Phlip | last post by:
Javascripters: I have an outer page and an inner iframe. The outer page calculates some javascript, and wants the inner frame to run it. The inner frame should hit a page on the same (private)...
4
by: Academia | last post by:
I get the following watch message: tsSource.Text.ToUpper() This expression causes side effects and will not be evaluated string The Text is &Edit
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.