Velocity Reviews > RE: if does not evaluate

# RE: if does not evaluate

Robert Brewer
Guest
Posts: n/a

 06-05-2004
Jim Newton wrote:
> A question that has bothered me ever since looking at python
> the first time is that not everything evaluates to something.

Are you only bothered by it because there's no ifelse operator?

> 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?

Only Zen standards:

1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
6. Sparse is better than dense.

> 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.

You'll first have to convince many people that this is a "problem" to be
"solved". Why is your solution "better than":

if something:
x = expr1
else:
x = expr2

Then you need to show why the alternatives aren't good enough:

If your main desire is to code in some other language while still using
Python, write your own VB-style IIf function:

>>> def IIf(expression, truepart, falsepart):

.... if expression:
.... return truepart
.... return falsepart
....
>>> x = IIf('something', 'expr1', 'expr2')
>>> x

'expr1'

If your main desire is to provide a one-liner, use a tuple (which is
still ugly):

>>> x = ('expr2', 'expr1')[bool('something')]
>>> x

'expr1'

Or a mapping (which is prettier, but still uglier than the regular if:
else:

>>> x = {True: 'expr1',

.... False: 'expr2'}[bool('something')]
>>> x

'expr1'

Or a more generic morphing function:

>>> def surjection(input, mappings):

.... mappings = dict(mappings)
.... return mappings[input]
....
>>> x = surjection(bool('something'), {True: 'expr1', False: 'expr2'})
>>> x

'expr1'
>>> x = surjection(bool('something'), [(True, 'expr1'), (False,

'expr2')])
>>> x

'expr1'
>>> x = surjection('something', [('something', 'expr1'), ('other',

'expr2'), ('than', 'expr2')])
>>> x

'expr1'

Robert Brewer
MIS
Amor Ministries
http://www.velocityreviews.com/forums/(E-Mail Removed)

Jim Newton
Guest
Posts: n/a

 06-06-2004
how do you put an if or a for inside a lambda?

The expression i'm suggesting as evaluatable
could be passable to a function call as well,
as being mappable, or usable inside a lambda.
It seems better to me in that it stops being
a special case.

E.g.,

myfun( ( if cond1 expr1: else expr2 ),
( if cond2 expr3: else expr4 ))

-jim

>>
>>I.e., x = if something:
>> expr1
>> else:
>> expr2
>>
>>parentheses would of course be optional as they are for
>>all expressions.

>
>
> You'll first have to convince many people that this is a "problem" to be
> "solved". Why is your solution "better than":
>
> if something:
> x = expr1
> else:
> x = expr2
>

Terry Reedy
Guest
Posts: n/a

 06-06-2004

"Jim Newton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> how do you put an if or a for inside a lambda?

By wrapping them in other functions; otherwise you don't.

lambda *args: expr #abbreviates (minus the function name)
def f(*args): return expr

If your function is more complicated than that, don't try to abbreviate.
Use def.

Terry J. Reedy

Dan Bishop
Guest
Posts: n/a

 06-06-2004
Jim Newton <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> how do you put an if or a for inside a lambda?

I'm not quite sure what you'd expect "for" to evaluate to, but you
might want to take a look at list comprehensions:

[expr(var) for var in seq if cond(var)]

For "if", you can write "(F, T)[bool(C)]" or "(C and [T] or [F])[0]".
The latter has the advantage of short-circuiting.

Jim Newton
Guest
Posts: n/a

 06-07-2004
well, i accept the fact that python offers some simple
concepts and you do a lot with simple concepts, but why
can't you put an if inside a lambda? if "if" were evaluatable
then you would be able to do that. it seems to me.

lambda seem to work for very simple things but not for
anything substantial.

Terry Reedy wrote:
> "Jim Newton" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>how do you put an if or a for inside a lambda?

>
>
> By wrapping them in other functions; otherwise you don't.
>
> lambda *args: expr #abbreviates (minus the function name)
> def f(*args): return expr
>
> If your function is more complicated than that, don't try to abbreviate.
> Use def.
>
> Terry J. Reedy
>
>
>
>

Jim Newton
Guest
Posts: n/a

 06-07-2004
for compressions are quite nice but you are still pretty
limited about what you can put inside the for. you cannot
put any code otherwise acceptable in the language. For example
what if i need two, three or four statements inside the for?

another question, is why are "if" compressions not supported?

x = [ if some_function() else some_other_function() ]

or better

x = [ if cond1():
elsif cond2():
else: cond3() ]

whouldn't that be inkeeping with the compression syntax?

-jim

Dan Bishop wrote:
> Jim Newton <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>
>>how do you put an if or a for inside a lambda?

>
>
> I'm not quite sure what you'd expect "for" to evaluate to, but you
> might want to take a look at list comprehensions:
>
> [expr(var) for var in seq if cond(var)]
>
> For "if", you can write "(F, T)[bool(C)]" or "(C and [T] or [F])[0]".
> The latter has the advantage of short-circuiting.

Skip Montanaro
Guest
Posts: n/a

 06-07-2004

Jim> lambda seem to work for very simple things but not for anything
Jim> substantial.

Right. Like Terry said, for anything more substantial use a named function.
Lambda was never intended to be a replacement for def, and Python is
fundamentally not a functional language (in the Haskell/Lisp sense of the
term), so powerful anonymous functions are generally not needed. In fact,
as list comprehensions and generator expressions take root, lambda's use
dwindles.

Skip

Nicolas Fleury
Guest
Posts: n/a

 06-08-2004
Robert Brewer wrote:
> If your main desire is to code in some other language while still using
> Python, write your own VB-style IIf function:

All the solutions you propose don't work (except the long form). You
cannot code an "iif" function. Test:

def iif(cond, expr1, expr2):
if cond: return expr1
return expr2
def foo(x): return iif(x == 0, 0, 1.0/x)
foo(4)
foo(0)

See PEP 308.

Regards,
Nicolas

Jacek Generowicz
Guest
Posts: n/a

 06-09-2004
Skip Montanaro <(E-Mail Removed)> writes:

> Right. Like Terry said, for anything more substantial use a named function.
> Lambda was never intended to be a replacement for def, and Python is
> fundamentally not a functional language (in the Haskell/Lisp sense of the
> term), so powerful anonymous functions are generally not needed.

I wish people would stop making such self-fulfilling prophecies.

You[*] don't use fully general anonymous closures, because the
languages you use do not provide you that tool. Because such beasts,
do not form part of your normal programming arsenal, you don't use
them. Because you don't use them, you rarely see them used. Because
you rarely see them used, you conclude that they are not needed. You
you make the mistake of confusing your lack of imagination with some
inherent quality of the language in question. The fact is that people
who are used to using powerful programming techinques which you may
not use yourself, _do_ miss their presence in languages in which you
consider their absence to be a feature.

Python has many qualities, but let's stop kidding ourselves that its
current state is some sort of global optimum in the space of
programming languages.

In spite of its many qualities, Python has a number of
shortcomings. Let's stop kidding ourselves that its shorcomings are
features.

Python's naff support for anonymous closures is a shortcoming, not a
feature.

[*] By "you" I mean "one", not "Skip Montanaro".

Jim Newton
Guest
Posts: n/a

 06-09-2004

Jacek Generowicz wrote:
> Because
> you rarely see them used, you conclude that they are not needed. You
> you make the mistake of confusing your lack of imagination with some
> inherent quality of the language in question.

very well said!!

>
>
> In spite of its many qualities, Python has a number of
> shortcomings. Let's stop kidding ourselves that its shorcomings are
> features.
>
> Python's naff support for anonymous closures is a shortcoming, not a
> feature.
>
>

When i was a child, i thought as a child, and understood as a child,
but when i became a man, i put childish things behind me.

When i was a child, i only programmed in BASIC. At that time
the only type of variables were global variables, there were no
function definitions, and no recursion. i could not imagine
that being a shortcomming, because i had never seen or used local
varaibles, and i could handle GOTO quite easily.
But i was very clever child, and did some powerful things
with BASIC even with those restrictions.

Python, being a simple and restrictive language as it is, is still
quite nice and easy to learn. It is nice indeed, but lacks some
features that could make it a much more elagant language.

On the other hand perhaps the problem is not that Python has
shortcomings, perhaps the real problem is that LISP is lacking
lots of useful and mature UNIX and internet specific libraries.

-jim

--
+------------------------------------------------------------------------+
| Jim E. Newton ((E-Mail Removed)) desk +49-(0)89-4563-1918 |
| Methodology Services Europe fax +49-(0)89-4563-1819 |
| Cadence Design Systems GmbH Munich Germany |
| |
| If you won't do it in the rain, you won't do it. |
+------------------------------------------------------------------------+