Return value of an assignment statement?

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Feb 27 15:19:38 CET 2008


Paddy a écrit :
> On 21 Feb, 23:33, "bruno.desthuilli... at gmail.com"
> <bruno.desthuilli... at gmail.com> 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)
>>>         else if m = pat.match(other_string):
>>>             do_other_thing(m)
>>>         else:
>>>             do_default_thing()
>> What you want is:
>>
>> for astring, afunc in ((some_string, do_something), (other_string,
>> do_other_thing)):
>>     m = pat.match(astring)
>>     if m:
>>         afunc(m)
>>         break
>> else:
>>     do_default_thing()
> 
> The Bruno transform? :-)

I do not claim any paternity on this idiom. FWIW, must be (in one or 
another variant) in quite a lot of texts / posts / sites / any other 
resource about Python.




More information about the Python-list mailing list