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
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.
Wow, that is clever!
Generators probably. It’s the one thing i genuinely miss about python when i work in rust.
impl Iterator for YourType
?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.
cool!
You’re right. Having just
yield sth
in a function instead of defining the data structure is such a game changer.
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.
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.
Context managers primarily but quickly followed up by decorators. I primarily develop in Go nowadays and miss them dearly.
Dataclasses and
__post_init__
, what should have been the default.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.
The recent addition of match case has been a blast for me.
Another useful one is static methods.
I just got into OOP with classes and stuff and I will say that
@staticmethod
is super nice from an organizational standpoint
decorators and the
match
statement for pattern matching. Finally have aswitch
statement, even if Guido didn’t want it. It’s just an advanced switch statement.Decorators are one of most useful features I’m using most frequently.
Yeah, I’m just learning about decorators in classes, and they’re super cool!
deleted by creator
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