python one line function definition

If you must have one line just make it a lambda:

perms = lambda xs: (list(x) for x in itertools.permutations(xs))

Quite often, when you have a short for loop for generating data you can replace it with either list comprehension or a generator expression for approximately the same legibility in slightly less space.


In your case, I am not sure. But with some functions you can achive this by using semicolons.

>>> def hey(ho): print(ho); print(ho*2); return ho*3
...
>>> hey('you ')
you
you you
'you you you '

Yes, there are restrictions. No, you can't do that. Simply put, you can skip one line feed but not two.

See here.

The reason for this is that it would allow you to do

if test1: if test2: print x
else:
    print y

Which is ambiguous.

Tags:

Python