Assignment-in-conditional

Discussion in 'Python' started by xamdam, Aug 31, 2006.

  1. xamdam

    xamdam Guest

    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
    xamdam, Aug 31, 2006
    #1
    1. Advertising

  2. Fredrik Lundh, Aug 31, 2006
    #2
    1. Advertising

  3. xamdam

    Neil Cerutti Guest

    On 2006-08-31, 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-i-use-an-assignment-in-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
    Neil Cerutti, Aug 31, 2006
    #3
  4. xamdam

    Georg Brandl Guest

    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
    Georg Brandl, Aug 31, 2006
    #4
  5. xamdam

    xamdam Guest

    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-i-use-an-assignment-in-an-expression
    >
    > </F>
    xamdam, Aug 31, 2006
    #5
  6. xamdam

    Simon Forman Guest

    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-i-use-an-assignment-in-an-expression
    > >
    > > </F>


    There are two recipies in the cookbook for doing this:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/202234
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66061

    HTH,
    ~Simon
    Simon Forman, Sep 1, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. itsme
    Replies:
    1
    Views:
    1,625
    Ralf Hildebrandt
    Jul 23, 2003
  2. Anand P Paralkar
    Replies:
    2
    Views:
    9,385
    Srinivasan Venkataramanan
    Aug 4, 2003
  3. Johnsy Joseph

    Conditional assignment to signals

    Johnsy Joseph, Sep 21, 2004, in forum: VHDL
    Replies:
    8
    Views:
    4,879
    Raghavendra
    Sep 24, 2004
  4. Replies:
    2
    Views:
    2,790
  5. Alec S.
    Replies:
    10
    Views:
    10,145
    Alec S.
    Apr 16, 2005
Loading...

Share This Page