Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Assignment-in-conditional

Reply
Thread Tools

Assignment-in-conditional

 
 
xamdam
Guest
Posts: n/a
 
      08-31-2006
I am not sure if this came up before, but I would love to have an
'assignment-in-conditional' form in python, e.g

pat = re.compile('something')

if m = pat.match(s):
m.group(1)

Of course there is some concern about accidentally using '=' instead of
'=='. One possible solution is to do what the 'with' statement does:

if pat.match(s) as m:
...

a little ugly but not too much worse that with itself..

what do you guys think?

- max

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-31-2006
xamdam wrote:

> I am not sure if this came up before, but I would love to have an
> 'assignment-in-conditional' form in python, e.g


it's a FAQ, so it has probably come up before:

http://pyfaq.infogami.com/why-can-t-...-an-expression

</F>

 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      08-31-2006
On 2006-08-31, Fredrik Lundh <(E-Mail Removed)> wrote:
> xamdam wrote:
>
>> I am not sure if this came up before, but I would love to have an
>> 'assignment-in-conditional' form in python, e.g

>
> it's a FAQ, so it has probably come up before:
>
> http://pyfaq.infogami.com/why-can-t-...-an-expression


Python saves me from ever making the assignment-as-conditional
mistake, but I invented the conditional-as-assignment mistake to
compensate.

--
Neil Cerutti
A billion here, a billion there, sooner or later it adds up to
real money. --Everett Dirksen
 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      08-31-2006
xamdam wrote:
> I am not sure if this came up before, but I would love to have an
> 'assignment-in-conditional' form in python, e.g
>
> pat = re.compile('something')
>
> if m = pat.match(s):
> m.group(1)
>
> Of course there is some concern about accidentally using '=' instead of
> '=='. One possible solution is to do what the 'with' statement does:
>
> if pat.match(s) as m:
> ...
>
> a little ugly but not too much worse that with itself..
>
> what do you guys think?


It has been proposed before (a few times even), and Guido doesn't like it.

Georg
 
Reply With Quote
 
xamdam
Guest
Posts: n/a
 
      08-31-2006
Thanks for the FAQ, and for the 'casm

What do you think about using alternative syntax (something like 'as')

- max

Fredrik Lundh wrote:
> xamdam wrote:
>
> > I am not sure if this came up before, but I would love to have an
> > 'assignment-in-conditional' form in python, e.g

>
> it's a FAQ, so it has probably come up before:
>
> http://pyfaq.infogami.com/why-can-t-...-an-expression
>
> </F>


 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      09-01-2006
xamdam wrote:
> Thanks for the FAQ, and for the 'casm
>
> What do you think about using alternative syntax (something like 'as')
>
> - max
>
> Fredrik Lundh wrote:
> > xamdam wrote:
> >
> > > I am not sure if this came up before, but I would love to have an
> > > 'assignment-in-conditional' form in python, e.g

> >
> > it's a FAQ, so it has probably come up before:
> >
> > http://pyfaq.infogami.com/why-can-t-...-an-expression
> >
> > </F>


There are two recipies in the cookbook for doing this:

http://aspn.activestate.com/ASPN/Coo.../Recipe/202234
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66061

HTH,
~Simon

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments