• frezik
    link
    fedilink
    arrow-up
    35
    ·
    1 year ago

    People used to argue that Python was incredibly readable. Then I started seeing shit like this.

      • frezik
        link
        fedilink
        arrow-up
        7
        ·
        1 year ago

        It jams far too much on one line. Break it up. It’s a mistake I see a lot on Python.

    • addie@feddit.uk
      link
      fedilink
      arrow-up
      15
      ·
      1 year ago

      I think that Python has a bit of a ‘Microsoft Word’ thing on the go. You know how your own docs are completely editable and print fine, but everyone else’s are a complete fucking disaster and pressing a single key will screw up the formatting of the whole document? Your own Python code is full of sensible idioms and pragmatic naming conventions, but everyone else’s was plainly written while on mushrooms.

  • AngrilyEatingMuffins@kbin.social
    link
    fedilink
    arrow-up
    25
    ·
    1 year ago

    The image you’ve uploaded is a humorous take on a programming practice common among Python developers. It shows a list comprehension, which is a concise way to create lists in Python. The joke is that nobody prompted the Python programmers to use a complex or sophisticated feature, yet they are using it anyway, which implies that Python programmers tend to use list comprehensions frequently and perhaps even when they are not strictly necessary. List comprehensions are a popular feature in Python because they can make the code more readable and expressive, and this meme plays on the idea that Python programmers might be eager to use them at every opportunity.

  • riodoro1@lemmy.world
    link
    fedilink
    arrow-up
    23
    ·
    1 year ago

    Python has great list comprehension. Too bad it’s incomprehensible to humans.

    But seriously whoever writes those cool oneliners is a shitty programmer. Life is not code golf, fuck off with your smart ass.

    • Bonsoir@lemmy.ca
      link
      fedilink
      English
      arrow-up
      14
      ·
      edit-2
      1 year ago

      What would be the alternative? (assuming that you want to do the loop yourself)

      new_results = []
      for result in results:
          if result:
              new_results.append(result)
      results = new_results
      

      or else

      for result in results:
          if not result:
              results.remove(result)
      

      which doesn’t do the exact same thing.
      Honestly, this list comprehension is much faster to read and quite easy to understand.
      I think we could rename the “result” variable “x” or “res” and it would be less confusing though.

  • Synthead@lemmy.world
    link
    fedilink
    English
    arrow-up
    14
    ·
    1 year ago

    Ruby has a method for this :)

    [1] pry(main)> vars = ["one", "two", nil, "three"]
    => ["one", "two", nil, "three"]
    [2] pry(main)> vars.compact
    => ["one", "two", "three"]
    

    In Ruby, 0 and "" is truthy, so this would be a little different than the Python interpretation. You could filter with #select, but you’d typically write your code around this instead.

      • Die Martin Die@sh.itjust.works
        link
        fedilink
        arrow-up
        8
        ·
        1 year ago

        Lua is the same. Only false and nil are “falsey”.

        I remember I fixed a bug in a mod for Minetest that assumed 0 was false and caused an infinite loop.

      • Synthead@lemmy.world
        link
        fedilink
        English
        arrow-up
        4
        ·
        edit-2
        1 year ago

        Yup :) Everything in Ruby inherits Object, too. It’s a really neat language, and when you get accustomed to it, you might wonder why some other languages aren’t written like it.

        For the 0 value being truthy, consider that Ruby is a dynamic language. Imagine if you asked a user how many motorcycles they own. If they answer, you’ll have an Integer. If they don’t, you’ll have nil, from NilClass. 0 is just as valid of an answer as 2, so you can do something like this:

        raise NoResponse unless motorcycles
        
        save_motorcycles(motorcycles)
        
      • DarkenLM@kbin.social
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        And people bash Javascript as if it was the devil when thinks like this exist on other languages.

    • calcopiritus@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      Python also has about 9000 alternatives that are better than this.

      Allowing anything other than variables, number literals, and ‘:’ inside list indices was a mistake.

  • mvirts@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    1 year ago

    I’ve written this more times than I can remember 😹 who needs filter anyway? Gotta use up all this ram.

  • itslilith@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    1 year ago

    It does! it takes a list (or other iterator) and filters out all values that are cast to boolean True. The same could be archived with

    results = list(filter(bool, results))
    
  • Nevoic@lemm.ee
    link
    fedilink
    arrow-up
    9
    ·
    edit-2
    1 year ago

    Python’s disdain for the industry standard is wild. Every other language made in the last 20 years has proper filtering that doesn’t require collecting the results back into a list after filtering like Java (granted it’s even more verbose in Java but that’s a low bar).

    If Python had modern lambdas and filter was written in an inclusion or parametric polymorphic way, then you could write:

    new_results = results.filter(x -> x)
    

    Many languages have shorthands to refer to variables too, so it wouldn’t be impossible to see:

    new_results = results.filter(_)
    

    Of course in actual Python you’d instead see:

    new_results = list(filter(lambda x: x, results))
    

    which is arguably worse than

    new_results = [x for x in results if x]