Return value of an assignment statement?

Paul Rubin http
Fri Feb 22 04:42:07 EST 2008


Carl Banks <pavlovevidence at gmail.com> writes:
> > >>         import re
> > >>         pat = re.compile('some pattern')
> > >>         if m = pat.match(some_string):
> > >>             do_something(m)
> >...
> Don't be ridiculous.  Assignment operators are maybe one of the worst
> things in existence, but this particular use case (running a sequence
> of tests like the above) is perfectly useful and good.

I've been known to do stuff like:

   class xmatch:
      def set(self, v):
          self.v = v
          return v

then:

   s = xmatch()

   if s.set(pat.match(some_string)):
      do_something(s.v)
   elif s.set(pat.match(other_string)):
      do_other_thing(s.v)
   ...



More information about the Python-list mailing list