The thing I love about python is it’s elegance; and I thing that is partially due to its syntactic sugar. Between list comprehensions, destructuring, enumerators, generators with yield, and a bunch more, what is your favorite

  • @UndercoverUlrikHD
    link
    11
    edit-2
    1 year ago

    I use python a lot to automate various file management on my computer, so

    @contextmanager
    def cd(newdir) 
        prevdir = os.getcwd()
        os.chdir(os.path.expanduser(newdir)
        try:
            yield
        finally:
            os.chdir(prevdir)
    

    is my most reused function as it allows me write

    with cd(newdir) 
        #stuff
    

    So I’d definitely go with contextmanager/yield as my favourite syntax sugar.

  • @Walnut356
    link
    81 year ago

    Generators probably. It’s the one thing i genuinely miss about python when i work in rust.

      • @tatterdemalion
        link
        31 year ago

        That’s not quite the same as a generator. Iterators require explicit returns to yield control, and this involves dropping the entire stack frame.

        True generators allow one to yield, which pauses the function and allows it to be resumed. The most similar thing to this in Rust is an async block/fn, but there is ongoing effort to generalize this so you could create an iterator from a generator.

    • @[email protected]
      link
      fedilink
      111 months ago

      You’re right. Having just yield sth in a function instead of defining the data structure is such a game changer.

  • flatbield
    link
    fedilink
    English
    7
    edit-2
    1 year ago

    I come from the school of thought that programs should look like what they do. So I have mixed felling about the complexity increase in Python. Also about those pushing types.

    That said the subrange notation is one of the most important. I have used everything you mentioned in the appropriate places though. I find myself using list comprehensions and also the format method more often these days.

    • @SpeakinTelnet
      link
      11 year ago

      I have mixed felling about the complexity increase in Python. Also about those pushing types.

      I’m with you on that. Type hints are supposed to be just that, hints. I never saw the appeal of forcing python into a statically typed language with tools like pyrights.

  • @rushaction
    link
    51 year ago

    Context managers primarily but quickly followed up by decorators. I primarily develop in Go nowadays and miss them dearly.

  • @lavafroth
    link
    51 year ago

    Dataclasses and __post_init__, what should have been the default.

  • @lysdexic
    link
    English
    41 year ago

    I have to go with type hints. Even when the project doesn’t have any static type check in place, they help reason about the code.

  • @SpeakinTelnet
    link
    41 year ago

    The recent addition of match case has been a blast for me.

    • @[email protected]OP
      link
      fedilink
      1
      edit-2
      1 year ago

      I just got into OOP with classes and stuff and I will say that @staticmethod is super nice from an organizational standpoint

  • @onlinepersona
    link
    English
    11 year ago

    decorators and the match statement for pattern matching. Finally have a switch statement, even if Guido didn’t want it. It’s just an advanced switch statement.

  • @namingthingsiseasy
    link
    11 year ago

    List comprehensions, dict comprehensions, set comprehensions, and generator expressions. They just make it so fast and easy to transform data.

    Also just generators in general. I really like how they save unnecessary computation (when used prudently of course!).

    And I’m also a big fan of list indexing, especially how you can extract the last three elements of a list using l[-3:]. Very elegant syntax