this post was submitted on 16 Oct 2023
7 points (88.9% liked)

Python

6402 readers
9 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] UndercoverUlrikHD 11 points 1 year ago* (last edited 1 year ago) (1 children)

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.

[โ€“] lavafroth 1 points 1 year ago

Wow, that is clever!