Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69202 > unrolled thread

Python language hack for C-style programmers [DO NOT USE!] :-)

Started byTim Chase <python.list@tim.thechases.com>
First post2014-03-27 11:08 -0500
Last post2014-03-27 11:08 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Python language hack for C-style programmers [DO NOT USE!] :-) Tim Chase <python.list@tim.thechases.com> - 2014-03-27 11:08 -0500

#69202 — Python language hack for C-style programmers [DO NOT USE!] :-)

FromTim Chase <python.list@tim.thechases.com>
Date2014-03-27 11:08 -0500
SubjectPython language hack for C-style programmers [DO NOT USE!] :-)
Message-ID<mailman.8622.1395936527.18130.python-list@python.org>
Multiple times, I've seen someone want something like what C-style
languages offer where assignment is done in a test, something like

  if (m = re.match(some_string)):
    do_something(m)

So when I stumbled upon this horrific atrocity of language abuse and
scope leakage, I thought I'd share it.

  if [m for m in [regex.match(some_string)] if m]:
    do_something(m)

And presto, assignment in an if-statement.  It even "works" in
while-statements too:

  while [m for m in [regex.match(some_string)] if m]:
    some_string = do_something(m)

That said, it's ugly, far more opaque/inefficient than the traditional

  m = regex.match(some_string)
  if m:
    do_something(m)

and if I ever caught someone on my dev teams doing this in production
code, their backside would receive a stern conversation with my
footwear.

Friends don't let friends program C in Python. ;-)

-tkc



[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web