I just love writing these unreadable python lambdas

Not exactly a help post

Can you guess what this one does or how exactly it does it?

rgenkeys = lambda s=2048, e=65537: ((lambda obj1, obj2: (obj1, obj2.public_key()))(*((lambda obj: (obj, obj))(rsa.generate_private_key(public_exponent=e, key_size=s)))))

No guessing required.

  1. It annoys and frustrates the reader.
  2. By being an obnoxiously obfuscated one-liner using multiple nested lambdas.

Here is a more or less exact translation of the code:

def rgenkeys(s=2048, e=65537):
    def duplicate(obj):
        return (obj, obj)
    def inner(obj1, obj2):
        return obj1, obj2.public_key()
    t = rsa.generate_private_key(public_exponent=e, key_size=s)
    return inner(*duplicate(t))

And here is how it should have been written:

def rgenkeys(s=2048, e=65537):
    t = rsa.generate_private_key(public_exponent=e, key_size=s)
    return (t, t.public_key())
3 Likes

Hello, @DiamondDemon, and welcome to Python Software Foundation Discourse!

:wink: It causes the reader to submit the following at the interactive Python prompt, and then to consider which lines among the response can serve as guidance:

>>> import this

Most code is read more than it was written, so saving a little time while writing things is a false economy.

I can’t imagine that this was written faster than a more simple function would have been. It looks like it was deliberately obfuscated just for the sake of posting here.

In a past job, my boss’, boss’, boss’, boss said to “never attribute to malice that which can be attributed to stupidity”.