How to do this in Python...
Erik Max Francis
max at alcyone.com
Thu Jan 23 19:58:26 EST 2003
Michael Tiller wrote:
> I'm puzzled by what seems like a missing piece of functionality in
> Python.
> I suspect there is a simple way to do what I want to do and I'm
> guessing
> someone here can point it out to me. Here is the issue:
>
> In C++, I could write a statement like this:
>
> if (match=re.match(pattern, string))
> // do something with the match object
>
> But I cannot figure out how to do the equivalent thing in Python.
You cannot (without winning obfuscation contests, anyway). Assignment
is a statement, and if expects expressions. This was done as a
deliberate act to avoid the common newbie mistake in C or C++ of writing
if (a = b) ...
when you meant
if (a == b) ...
Whether or not you agree with the philosophy behind the decision, it's
in the language. The Pythonic way to do the same thing is an assignment
and then a separate test:
a = b
if a: ...
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ An undevout astronomer is mad.
\__/ Edward Young
Bosskey.net: Quake III Arena / http://www.bosskey.net/q3a/
A personal guide to Quake III Arena.
More information about the Python-list
mailing list