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.