Return value of an assignment statement?
Jeff Schwab
jeff at schwabcenter.com
Thu Feb 21 19:23:23 EST 2008
Steve Holden wrote:
> mrstephengross wrote:
>>> What you can't do (that I really miss) is have a tree of assign-and-test
>>> expressions:
>>> import re
>>> pat = re.compile('some pattern')
>>> if m = pat.match(some_string):
>>> do_something(m)
>>
>> Yep, this is exactly what I am (was) trying to do. Oh well.... Any
>> clever ideas on this front?
>>
> The syntax is the way it is precisely to discourage that kind of clever
> idea. Of course, people nevertheless manage to work around the
> restriction to try and make their Python read like some other language
> they are more familiar with, and most of the time they get away with it.
>
> The fat remains that in programming there *is* such a thing as being too
> clever, and Python's syntax deliberately discourages that.
This isn't "clever." It's what most of us were taught, from the
beginning, was best practice. In some languages (including C and C++)
the above is extremely common and perfectly acceptable.
More information about the Python-list
mailing list