Live data from Hacker News

Python built-ins worth learning

treyhunner.com

21–30 of 69 posts

Re: Python built-ins worth learning

#21
post #17

I disagree with only one thing in this article. Frozenset is super useful. You should prefer frozenset over set by default imo. Immutable objects are safer to work with, often easier to work with (frozenset is hashable, you can key a dict with it), and sets come up all the time in a lot of work. You can get by with a sorted list, but sets are nicer and faster.

Great point. I do agree that those who use sets heavily should probably know about frozenset. :) I've never very rarely seen these used in production code though. I'm also not as strongly biased toward them because set objects in my own code are often pretty short-lived so their mutability rarely matters.

I found myself using them more and more often as I started to adopt type annotations.

As you do that, you start to realize that you don't need a MutableMapping. Just a Mapping will do, much less a Dict. Same with Set.

Re: Python built-ins worth learning

#22
I’ve been writing Python for a little over a year mostly as glue code and scripting around AWS. I always had a sneaking suspicion that there was more I needed to learn and that I wasn’t doing things the “Pythonic” way.

This article confirmed my suspicions. Great post.

Re: Python built-ins worth learning

#25
post #24

Relatedly, I highly recommend Dave Beazley's "Built-In Superheroes" talk: https://www.youtube.com/watch?v=j6VSAsKAj98 I rewatch it every 6 months or so and usually learn a new trick or two.

Any idea what tool he's using for the "slides"?

believe it or not, that's iTerm2 and the ipython REPL.

To borrow a phrase from Avatar: The Last Airbender, dabeaz is, uh, a mad genius.

Re: Python built-ins worth learning

#26
post #17

I disagree with only one thing in this article. Frozenset is super useful. You should prefer frozenset over set by default imo. Immutable objects are safer to work with, often easier to work with (frozenset is hashable, you can key a dict with it), and sets come up all the time in a lot of work. You can get by with a sorted list, but sets are nicer and faster.

Great point. I do agree that those who use sets heavily should probably know about frozenset. :) I've never very rarely seen these used in production code though. I'm also not as strongly biased toward them because set objects in my own code are often pretty short-lived so their mutability rarely matters.

Troy, if you are reading this: please lower your output a bit. I like your stuff a lot but I had to start filtering out your emails because they were relentless.

Re: Python built-ins worth learning

#27

I disagree with only one thing in this article. Frozenset is super useful. You should prefer frozenset over set by default imo. Immutable objects are safer to work with, often easier to work with (frozenset is hashable, you can key a dict with it), and sets come up all the time in a lot of work. You can get by with a sorted list, but sets are nicer and faster.

I completely agree. Working on complex enterprise Python applications, a pretty significant suite of bugs comes from long-lived, mutable objects. We’ve generally had good luck with preferring immutability wherever possible, both using builtins (tuples rather than lists when possible, frozenset) and custom classes. I just wish frozendict was part of the stdlib!

Re: Python built-ins worth learning

#28
post #24

Earlier quoted context omitted.

Any idea what tool he's using for the "slides"?

believe it or not, that's iTerm2 and the ipython REPL. To borrow a phrase from Avatar: The Last Airbender, dabeaz is, uh, a mad genius.

I was really asking about the keywords he typed that showed the "slides" in the terminal. For instance, when he typed in "builtins" at the beginning, it cleared his screen and then showed a quick ASCII art image. I suppose he could have just built these functions, but if there is a tool that he might be using, that would be pretty handy.

Re: Python built-ins worth learning

#29
post #28

Earlier quoted context omitted.

believe it or not, that's iTerm2 and the ipython REPL. To borrow a phrase from Avatar: The Last Airbender, dabeaz is, uh, a mad genius.

I was really asking about the keywords he typed that showed the "slides" in the terminal. For instance, when he typed in "builtins" at the beginning, it cleared his screen and then showed a quick ASCII art image. I suppose he could have just built these functions, but if there is a tool that he might be using, that would be pretty handy.

no no, that's what i mean: he hacked the REPL up to hell and gone, and is actually using it to run his presentation. It's nuts. See ~04:20 where he briefly alludes to that in the talk.

Re: Python built-ins worth learning

#30
Sets are my #1 favourite Python built-in. I used to have these tedious imperative functions for doing change detection on collections. Then I learned sets and it turned into obvious code like:

added = b - a

removed = a - b

repeated = a & b

Etc.

Of course this is set theory and not Python specific. But still. Learn sets!

Post reply on HN