i++ in Python?

Alex Martelli aleax at aleax.it
Fri Jul 19 07:05:45 EDT 2002


David Eppstein wrote:

> In article <5606b639.0207181429.2c6d2569 at posting.google.com>,
>  otis_usenet at yahoo.com (OtisUsenet) wrote:
> 
>> Python newbie question.  How does one do something like this in
>> Python:
>> 
>>     # assign the value of j to i and then increment j by 1
>>     i = j++
> 
> Python is not designed for maximal terseness, nor for compatibility with
> C, so you need two statements to do this (well, maybe there's a way to
> do it in one, but the easiest-to-read and therefore best is with two):
> 
>       i = j
>       j += 1

A particularly mysterious, unreadable and useless way to "do it in one":

i = [ j for j in j, j+1 ][0]

Generally, I find that abusing list comprehensions is the easiest way
to produce obfuscated Python.  The specific "feature" of LCs that makes
them so suitable for obfuscation is their ability to rebind variables
in their for clauses.


Alex




More information about the Python-list mailing list