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

Python

6401 readers
18 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

all 20 comments
sorted by: hot top controversial new old
[–] 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!

[–] Walnut356 8 points 1 year ago (2 children)

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

[–] [email protected] 1 points 1 year ago

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

[–] lavafroth 1 points 1 year ago (1 children)

impl Iterator for YourType ?

[–] tatterdemalion 3 points 1 year ago (1 children)

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.

[–] lavafroth 1 points 1 year ago
[–] [email protected] 7 points 1 year ago* (last edited 1 year ago) (1 children)

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 1 points 1 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.

[–] lavafroth 5 points 1 year ago

Dataclasses and __post_init__, what should have been the default.

[–] rushaction 5 points 1 year ago

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

[–] lysdexic 4 points 1 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 4 points 1 year ago

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

[–] [email protected] 2 points 1 year ago (1 children)

Another useful one is static methods.

[–] [email protected] 1 points 1 year ago* (last edited 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

[–] [email protected] 1 points 1 year ago

Decorators are one of most useful features I'm using most frequently.

[–] [email protected] 1 points 1 year ago

Yeah, I'm just learning about decorators in classes, and they're super cool!

[–] onlinepersona 1 points 1 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 1 points 1 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