Newsgroups: comp.lang.python
From: boblat...@googlemail.com
Date: Wed, 21 May 2008 02:38:10 -0700 (PDT)
Local: Wed, May 21 2008 5:38 am
Subject: C-like assignment expression?
Hello,
I have an if-elif chain in which I'd like to match a string against if (match = my_re1.match(line): ...buy this is illegal in python. The other way is to open up an else: Thanks, You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Wed, 21 May 2008 11:46:48 +0200
Local: Wed, May 21 2008 5:46 am
Subject: Re: C-like assignment expression?
This might help:
----------- class Tester(object): def __call__(self, pattern): def __getattr__(self, name): test = Tester() if test("bar"): Diez You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: Ulrich Eckhardt <eckha...@satorlaser.com>
Date: Wed, 21 May 2008 12:14:22 +0200
Local: Wed, May 21 2008 6:14 am
Subject: Re: C-like assignment expression?
How about this (untested) code:
for re in (re1, re2, re3): This requires that "use it" means the same for each regular expression Uli -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: Bruno Desthuilliers <bruno.42.desthuilli...@websiteburo.invalid>
Date: Wed, 21 May 2008 13:14:38 +0200
Local: Wed, May 21 2008 7:14 am
Subject: Re: C-like assignment expression?
boblat...@googlemail.com a écrit :
> Hello, <ot> > I have an if-elif chain in which I'd like to match a string against > if (match = my_re1.match(line): Isn't it the third or fourth time this very same question pops up here ? Starts to look like a FAQ. </ot> The canonical solution is to iterate over a list of expression,function def use_match1(match): def use_match2(match): def use_match3(match): for exp, func in [ The alternate solution is Diez's Test object. HTH You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: Hrvoje Niksic <hnik...@xemacs.org>
Date: Wed, 21 May 2008 13:47:59 +0200
Local: Wed, May 21 2008 7:47 am
Subject: Re: C-like assignment expression?
Bruno Desthuilliers <bruno.42.desthuilli...@websiteburo.invalid>
writes: > The canonical solution is to iterate over a list of Although that solution is pretty, it is not the canonical solution > expression,function pairs, ie: because it doesn't cover the important case of "if" bodies needing to access common variables in the enclosing scope. (This will be easier in Python 3 with 'nonlocal', though.) The snippet posted by Diez is IMHO closer to a canonical solution to this FAQ. You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
Date: Wed, 21 May 2008 06:12:13 -0700 (PDT)
Local: Wed, May 21 2008 9:12 am
Subject: Re: C-like assignment expression?
On May 21, 1:47 pm, Hrvoje Niksic <hnik...@xemacs.org> wrote:
> Although that solution is pretty, it is not the canonical solution Hello everybody, > because it doesn't cover the important case of "if" bodies needing to > access common variables in the enclosing scope. (This will be easier > in Python 3 with 'nonlocal', though.) The snippet posted by Diez is > IMHO closer to a canonical solution to this FAQ. thanks for the various answers. I'm actually pretty puzzled because I Thanks again, PS: Since I'm testing only three REs, and I only need the match You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: cokofree...@gmail.com
Date: Wed, 21 May 2008 06:28:35 -0700 (PDT)
Local: Wed, May 21 2008 9:28 am
Subject: Re: C-like assignment expression?
On May 21, 3:12 pm, "boblat...@googlemail.com"
Is it really a lot to change to have it if my_re1.match(line): ? That reads clearly to me... You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Wed, 21 May 2008 15:39:10 +0200
Local: Wed, May 21 2008 9:39 am
Subject: Re: C-like assignment expression?
And wastes time. regular expressions can become expensive to match - doing
it twice might be hurtful. Diez You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: cokofree...@gmail.com
Date: Wed, 21 May 2008 06:43:59 -0700 (PDT)
Local: Wed, May 21 2008 9:43 am
Subject: Re: C-like assignment expression?
> And wastes time. regular expressions can become expensive to match - doing > Diez my_re3.match(line) ? You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Wed, 21 May 2008 16:09:47 +0200
Local: Wed, May 21 2008 10:09 am
Subject: Re: C-like assignment expression?
cokofree...@gmail.com wrote: How do you know *which* of the three has matched then? >> And wastes time. regular expressions can become expensive to match - >> Diez > match = (my_re1.match(line) or my_re2.match(line)) or Diez You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
| Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy |
| ©2013 Google |