Non-obvious possibilities of python syntax

Hi! All of us (ok not all :)) know python as wide-used, simple-to-read, easy-to-start programming language.

But at the same time python syntax allows to make really strange things.

Rewrite multiline function with lambda

As it’s known python lambda doesn’t support multiline code. But it can be simulated.

def f():
x = 'string'
if x.endswith('g'):
x = x[:-1]
r = ''
for i in xrange(len(x)):
if x[i] != 'i':
r += x[i]
return r
f()
-> 'strn'

Sounds strange but above function can be replaced with lambda-function:

(lambda: ([x for x in ['string']], x.endswith('g') and [x for x in [x[:-1]]], [r for r in ['']], [x[i] != 'i' and [r for r in [r+x[i]]] for i in xrange(len(x))], r)[-1])()
-> 'strn'

Never do that in production code :)

Ternary operator

Modern python gives you simple intuitive syntax:

b if a else c

But it also can be rewritten via:

(a and [b] or [c])[0]

(b, c)[not a]

Btw, next variant is incorrect:

a and b or c
True and [] or [1] -> [1], but: [] if True else [1] -> []

Remove repeated symbols via list comprehension

Let’s transform string x = 'tteesstt' to 'test'.

  1. compare a symbol with previous in original string:
''.join(['' if i and j == x[i-1] else j for i,j in enumerate(x)])

2. save previous symbol in temporary variable:

''.join([('' if i == a else i, [a for a in [i]])[0] for a in [''] for i in x])
''.join([('' if i == a.pop() else i, a.append(i))[0] for a in [['']] for i in x])

3. compare a symbol with previous in new string:

[(not r.endswith(i) and [r for r in [r+i]], r)[-1] for r in [''] for i in x][-1]

4. via reduce & lambda:

reduce(lambda a, b: a if a.endswith(b) else a + b, x)

Fibonacci via list comprehension

  1. save temporary values in list:
[(lambda: (l[-1], l.append(l[-1] + l[-2]))[0])() for l in [[1, 1]] for x in xrange(19)]
[(l[-1], l.append(l[-1] + l[-2]))[0] for l in [[1, 1]] for x in xrange(19)]

2. save temporary values in dict:

[i for x in [(lambda: (l['a'], l.update({'a': l['a'] + l['b']}), l['b'], l.update({'b': l['a'] + l['b']}))[::2])() for l in [{'a': 1, 'b': 1}] for x in xrange(10)] for i in x]
[i for x in [(l['a'], l.update({'a': l['a'] + l['b']}), l['b'], l.update({'b': l['a'] + l['b']}))[::2] for l in [{'a': 1, 'b': 1}] for x in xrange(10)] for i in x]

3. via reduce & lambda:

reduce(lambda a, b: a + [a[-1] + a[-2]], xrange(10), [1, 1])
reduce(lambda a, b: a.append(a[-1] + a[-2]) or a, xrange(10), [1, 1])

4. the quickest variant:

[l.append(l[-1] + l[-2]) or l for l in [[1, 1]] for x in xrange(10)][0]

Eternal cycle with list comprehension

[a.append(b) for a in [[None]] for b in a]

List slice tricks

  1. copy list:
l = [1, 2, 3]
m = l[:]
m
-> [1, 2, 3]

2. remove/replace any number of elements:

l = [1, 2, 3]
l[1:-1] = [4, 5, 6, 7]
l
-> [1, 4, 5, 6, 7, 3]

3. add elements to begin of list:

l = [1, 2, 3]
l[:0] = [4, 5, 6]
l
-> [4, 5, 6, 1, 2, 3]

4. add elements to end of list:

l = [1, 2, 3]
l[-1:] = [l[-1], 4, 5, 6]
l
-> [1, 2, 3, 4, 5, 6]

5. reverse list:

l = [1, 2, 3]
l[:] = l[::-1]

Replace method byte code

Python prohibits to replace instance method, making it as read-only property:

class A(object):
    def x(self):
print "hello"
a = A()
def y(self):
print "world"
a.x.im_func = y
-> TypeError: readonly attribute

But it can be replaced on byte-code level:

a.x.im_func.func_code = y.func_code
a.x()
-> 'world'

Note! It has influence not only to current instance but to class (if to be precise to function which is bound with class) and all other instances too:

new_a = A()
new_a.x()
-> 'world'

Mutable object as default function argument

To assign mutable object as default value to function argument is very dangerous and there are a lot of tricky questions on interviews about that. But it can be helpful as cache mechanism.

  1. Factorial:
def f(n, c={}):
if n in c:
return c[n]
if (n < 2):
r = 1
else:
r = n * f(n - 1)
c[n] = r
return r
f(10)
-> 3628800
f.func_defaults
({1: 1,
2: 2,
3: 6,
4: 24,
5: 120,
6: 720,
7: 5040,
8: 40320,
9: 362880,
10: 3628800},)

2. Fibonacci:

def fib(n, c={}):
if n in c:
return c[n]
if (n < 2):
r = 1
else:
r = fib(n - 2) + fib(n - 1)
c[n] = r
return r
fib(10)
-> 89
fib.func_defaults[0].values()
-> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]