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


Groups > comp.lang.python > #69202

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

Date 2014-03-27 11:08 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Python language hack for C-style programmers [DO NOT USE!] :-)
Newsgroups comp.lang.python
Message-ID <mailman.8622.1395936527.18130.python-list@python.org> (permalink)

Show all headers | View raw


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



Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web