Live data from Hacker News

Python built-ins worth learning

treyhunner.com

41–50 of 69 posts

Re: Python built-ins worth learning

#42
post #16
post #4

If you are reading this, the orange bar at the bottom 5% of my screen is hugely distracting. Please allow us to close it.

Thanks for noting this. I rarely look at my site on mobile and hadn't noticed how big that widget was. I just spent a few minutes making it less visually obtrusive. I may figure out more ways to improve it later.

Thank you!

Re: Python built-ins worth learning

#43

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 disagree with only one thing in this article. Frozenset is super useful.

Many of the "don't" callables are useful and I'd say I've used about half (`format` is especially nice as a more flexible version of `str`).

But Python's tendency to mutability and the `set` interface makes frozenset verbose to use & have higher overhead than sets[0]. Usually code is terser and just as clear using sets without mutating them. Which is a shame as of course you lose the safety of enforcing immutability (at runtime but still).

Then again, I guess if you're using a type checker it matters less: it shouldn't let you mutate a `set` if you've typed it as `Set` (rather than `MutableSet`).

[0] IIRC there's only one operation which is specifically optimised for frozensets: copy

Re: Python built-ins worth learning

#44

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!

Pycon 2019 about how to use Sets more in your every day code: https://www.youtube.com/watch?v=tGAngdU_8D8

Re: Python built-ins worth learning

#45
post #39

I always tend to recommend intermediate/advanced programmers to take a look at `functools` and `collections` packages - they are filled with amazing things that you haven't even though you need! My favourites are `functools.partial`, `collections.Counter` and `collections.defaultdict` that are used very often by people who are aware of their existance.

I use defaultdict so damn often, it honestly should even be a builtin in my opinion. You also forgot `itertools`, which imo is even more useful than `functools`. I use `chain` and `groupby` quite often. There's also `collections.deque` which is a quick linked list implementation if you need either a stack or a queue.

Re: Python built-ins worth learning

#46
post #39

I always tend to recommend intermediate/advanced programmers to take a look at `functools` and `collections` packages - they are filled with amazing things that you haven't even though you need! My favourites are `functools.partial`, `collections.Counter` and `collections.defaultdict` that are used very often by people who are aware of their existance.

I have been using defaultdict and Counter a lot lately, not much partial though. Also sets are another nice thing about python.

Re: Python built-ins worth learning

#48
post #39

I always tend to recommend intermediate/advanced programmers to take a look at `functools` and `collections` packages - they are filled with amazing things that you haven't even though you need! My favourites are `functools.partial`, `collections.Counter` and `collections.defaultdict` that are used very often by people who are aware of their existance.

I use defaultdict so damn often, it honestly should even be a builtin in my opinion. You also forgot `itertools`, which imo is even more useful than `functools`. I use `chain` and `groupby` quite often. There's also `collections.deque` which is a quick linked list implementation if you need either a stack or a queue.

Forgive me doesn’t deque mean double ended queue? Also why can’t a simple array behave like a list?

Re: Python built-ins worth learning

#49
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"?

Just watched his PyCon 2019 where he derives lambda calculus from scratch in Python, and he had similar shell magics going on there too. Great tutorial if you've never seen lambda calculus before, definitely brain twister.

https://www.youtube.com/watch?v=pkCLMl0e_0k

Re: Python built-ins worth learning

#50
post #28

Earlier quoted context omitted.

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.

I was at pycon when beaz programmed a set of raw socket server/client scripts running fibonnaci sequences LIVE from scratch in front of 1000 people using nothing but a text editor. The man is truly a mad genius.

https://youtu.be/MCs5OvhV9S4

Post reply on HN