if <assignment>:

André Næss andre.hates.spam at ifi.uio.no
Mon Nov 25 04:35:08 EST 2002


David Brown wrote:

> 
> "André Næss" <andre.hates.spam at ifi.uio.no> wrote in message
> news:arqm0r$s6f$1 at maud.ifi.uio.no...
>> When I started learning Python one of the things that surprised me was
> that
>> you couldn't do assignments inside the if clause, e.g.:
>>
>> if myvar = someFunction():
>>
>> My question is, what is the rationale for this? Is it a technical issue?
> Or
>> purely a matter of language design? I'm curious because I'm interested in
>> the design og programming languages, not because I want this behavior
>> changed in Pyton :)
>>
> 
> My guess is that the languages you are most familiar with are C and C++.

Java and PHP, actually, but that obviously doesn't make much of a difference 
:)

> The question should not be "why doesn't Python support 'if myvar =
> someFunc()'?", but "why *does* C support 'if (myvar = someFunc())' ?"  The
> answer is that C was designed with the sole aim of reducing keystrokes for
> the programmer, regardless of its effect on program readability and
> correctness.  

I've always felt one should use := as the assigment operator, and then = can 
be used as the equality operator.

But having read the thread I found on this subject I at least understood 
that in Python you will very rarely need this sort of thing, when I ran 
into this problem it was because I was thinking C-style. Now it's gonna be 
interesting to see if I ever run into a situation where I'd feel most 
comfortable doing an if <assignment>:, knowing that there are usually 
better ways of solving the problem.

André Næss



More information about the Python-list mailing list