Are one liners always pythonic?
MiguelMJ

MiguelMJ @miguelmj

About: CS Graduate| Software Engineer | Master's Student. Knowledge is only knowledge if it's shared.

Location:
Málaga, Spain
Joined:
Aug 9, 2020

Are one liners always pythonic?

Publish Date: Dec 16 '20
6 19

First of all, if you don't know what pythonic means, see this question.

I love clean code as much as anyone, but I think we all agree that there should be a limit on how much you put on a single line of code.

For example, looking at some examples on the Python wiki I came across elegant one liners like:

# Palindrome Python One-Liner
phrase.find(phrase[::-1])

# Find indices of x in a list lst
indices = [i for i in range(len(lst)) if lst[i]==x]
Enter fullscreen mode Exit fullscreen mode

but also some others that I don't find that much clean:

# Quicksort Python One-liner
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
Enter fullscreen mode Exit fullscreen mode

Do you have any criteria to limit your one-liners? Or don't you mind about the length of a line as long as it does the work?

Comments 19 total

  • Chris Greening
    Chris GreeningDec 16, 2020

    lol I've seen some pretty insane one-liners, I think my criteria is as long as it doesn't look ugly. Once a one-liner isn't almost immediately obvious I always break it into multiple lines

    • MiguelMJ
      MiguelMJDec 16, 2020

      That's more or less my criteria too.
      Out of curiosity, have other people read the one-liners you thought were obvious, but had to take their time to understand them?
      In my case, not many people see my code 😅 so I'm not sure if that's the same for everyone. In the end, the level of experience could make vary what is obvious and what no.

      • Chris Greening
        Chris GreeningDec 16, 2020

        I've kind of gotten a feel for what makes sense and what doesn't and this translates to readability for others as well. If the one-liner is longer than 79 characters that's usually a red flag and I always break my statements up as much as possible before introducing a complicated one liner. They usually end up reading more like pseudo code than anything too wild

        I will admit I've gotten lazy though and written crazy one liners for throwaway programs or programs under time constraint and will return months later and have trouble reading my own code lol

        • MiguelMJ
          MiguelMJDec 16, 2020

          That's one useful link!

      • Evan
        EvanDec 19, 2020

        The only people I’ve found who find one liners hard to navigate are people who aren’t already familiar with comprehensions. Which is always a fun thing to teach.

  • Chris Greening
    Chris GreeningDec 16, 2020

    Did someone say one-line Brainf*ck interpreter?

    ( lambda ins, sys: ( ( lambda f: f( f, 0, 0, data=[0]*100000, bp=( (lambda f: f(f, [], {}, 0)[0][1])( lambda g, bs, bp, ix: ( ((bs, bp),) if ix >= len(ins) else ( (g(g, bs + [ix], bp, ix+1), )[0] if ins[ix] == '[' else ( (g(g, bs[:-1], bp, ix+1), bp.update({ix: bs[-1], bs[-1]:ix}),)[0] if ins[ix] == ']' else g(g, bs, bp, ix+1) ) ) ) ) ) ) )( lambda g, ptr, i, data, bp: ( None if i >= len(ins) else g(g, *{ '>': lambda d, p, i, brace_pairs: (p + 1, i+1,), '<': lambda d, p, i, brace_pairs: (p - 1, i+1,), '+': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, (d[p]+1)%256))[:-1], '-': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, (d[p]-1)%256))[:-1], '.': lambda d, p, i, brace_pairs: (p, i+1, sys.stdout.write(chr(d[p])))[:-1], ',': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, ord(sys.stdin.buffer.read(1))))[:-1], '[': lambda d, p, i, brace_pairs: (p, brace_pairs[i] + 1 if not d[p] else i+1, ), ']': lambda d, p, i, brace_pairs: (p, brace_pairs[i] + 1 if d[p] else i+1, ), }[ins[i]](data, ptr, i, bp), data, bp) ) ) ) )( ''.join(i for i in open(__import__('sys').argv[1]).read() if i in {'<', '>', '+', '-', '.', ',', '[', ']'}), (__import__('sys'), __import__('sys').setrecursionlimit(100000))[0], )
    
    Enter fullscreen mode Exit fullscreen mode
    • MiguelMJ
      MiguelMJDec 16, 2020

      An insane language should have an insane interpreter!

    • Viper
      ViperDec 16, 2020

      Lambda within a lambda. You deserve a medal.

      • Chris Greening
        Chris GreeningDec 16, 2020

        lol can't claim the credit but definitely a bunch of clever tricks in there, here's another one-line Brainf*ck interpreter

        • Viper
          ViperDec 16, 2020

          Thanks for sharing. But oneliner is not that much readable. (I think)

          • MiguelMJ
            MiguelMJDec 16, 2020

            That's true, but I guess for some they are like a challenge. It happens the same with esoteric languages... We could say that Python becomes an esoteric language when you apply the rule "all in one line" hahaha

            • Chris Greening
              Chris GreeningDec 16, 2020

              Pretty recently I was writing a program that was going to be automated on a server with limited space so I crunched the program from 100 lines to like 15 awful one liners and it cut the size of the file down to like less than a kilobyte lol it was a fun challenge to see how small I could get the program and I could see people doing it for fun/proof of concept

  • Alain Van Hout
    Alain Van HoutDec 16, 2020

    Given that that second example packs quite an awful lot on a single line, I'm pretty it runs afoul of 'sparse is better than dense'.

    • MiguelMJ
      MiguelMJDec 16, 2020

      I like that philosophy!

  • JustinKaffenberger
    JustinKaffenbergerDec 16, 2020

    If you are:

    • Maintaining a long lived product
    • Attempting to scale your teams with new talent
    • Are willing to let go of your code wizard ego

    I highly recommend opting for more readable code.

    Otherwise! You get to be the bus factor of 1 for the piece of code.

  • Pacharapol Withayasakpunt
    Pacharapol WithayasakpuntDec 16, 2020

    One-liner is a joke. Isn't minified Javascript also a one liner?

    Python can also force one liner using semicolon. (And also break new line using back slash, IIRC.)

    That aside, readability is the most important, and after that, performance.

    Make mutable variables and loops if you have to. You don't need generator expressions every time, and it might confuse you.

    Also, named functions might be preferable to lambdas in any programming languages. It will make testing easier.

    • MiguelMJ
      MiguelMJDec 16, 2020

      I think that with semicolons doesn't count as one liner. Also, sometimes on liners improve readability.
      However, I think you're right about not having to use always generators. Just because you can doesn't mean that you should!

  • Evan
    EvanDec 19, 2020

    My one liners are almost never one liners.

    They always break into something like

    var = [
    # expression
    ]

    This increases readability quite a bit IMO.

Add comment