6 ms·
Python built-ins worth learning
- hprotagonist 7y agoRelatedly, I highly recommend Dave Beazley's "Built-In Superheroes" talk: https://www.youtube.com/watch?v=j6VSAsKAj98 https://www.youtube.com/watch?v=j6VSAsKAj98 I rewatch it every 6 months or so and usually learn a new trick or two.
- JoBrad 7y agoAny idea what tool he's using for the "slides"?
- hprotagonist 7y agobelieve 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.
- JoBrad 7y agoI 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.
- hprotagonist 7y agono 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.
- bshipp 7y agoI 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 https://youtu.be/MCs5OvhV9S4
- ehsankia 7y agoJust 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 https://www.youtube.com/watch?v=pkCLMl0e_0k
- santiagobasulto 7y agoAmazing post Trey, as usual. This is a very comprehensive list. I can't think of any other important function missing. My students will love this :)
- th 7y agoThanks Santiago! :)
- gloflo 7y agoIf you are reading this, the orange bar at the bottom 5% of my screen is hugely distracting. Please allow us to close it.
- ASalazarMX 7y agoI think it's because it overlaps a little of the content. I guess it makes some of us want to tidy it away.
- striking 7y agohttp://archive.is/lwklL http://archive.is/lwklL has it closed.
- th 7y agoThanks 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.
- gloflo 7y agoThank you!
- Splendor 7y ago> Many Python users rarely create classes. Creating classes isn’t an essential part of Python, though many types of programming require it. Huh. Okay.
- jackbrookes 7y agoData scientists for example.
- chasedehan 7y agoHuh? Every data scientist I have worked with who writes production code uses classes.
- emddudley 7y agoIt's really true. You can do a lot with data objects (dicts, lists) and stateless functions.
- civility 7y agoRich Hickey has some nice talks about why separating the data from the methods is really beneficial.
- corysama 7y agoThere’s a great Python talk out there titled “Stop writing classes”. It has several great demonstrations of Python code getting shorter, simpler and faster by converting code to just use the built-in containers.
- aaronchall 7y agoI did like that talk very much - and here was my response: > The Python Datamodel: When and how to write objects - https://www.youtube.com/watch?v=iGfggZqXmB0 https://www.youtube.com/watch?v=iGfggZqXmB0
- 7y ago
- joshuamorton 7y agoI 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.
- kitotik 7y agoHa! That was the thing I was most excited to learn about. I’ve never come across it in any codebases I’ve worked on, but will definitely begin training myself to use it by default over a normal set.
- th 7y agoGreat 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.
- joshuamorton 7y agoI 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.
- toyg 7y agoTroy, 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.
- mplanchard 7y agoI 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!
- rurp 7y agoThis article covers a lot of good material and I like the breakdown of categories from essential > maybe you'll look this up some day. My one minor suggestion would be to cover getattr and related methods sooner. I stumbled across that one pretty early on as I was learning programming and it was a serious "Ah ha!" moment. Dynamic lookups with a sensible default value are useful in countless contexts.
- scarface74 7y agoI’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.
- purplezooey 7y agoWish Python would get 'study' from Perl.
- tyingq 7y agoIt sorta does, now that study() is a no-op in Perl: https://perldoc.perl.org/functions/study.html https://perldoc.perl.org/functions/study.html
- mixmastamyk 7y agoWhat is the use case?
- Waterluvian 7y agoSets 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!
- joatmon-snoo 7y agofrozenset is a really nice one for enforcing immutabolity :)
- ForHackernews 7y agoFrozensets can also be keys in a dictionary.
- ehsankia 7y agoPycon 2019 about how to use Sets more in your every day code: https://www.youtube.com/watch?v=tGAngdU_8D8 https://www.youtube.com/watch?v=tGAngdU_8D8
- ggm 7y agowhat was fascinating to me was how I fitted into the MUST SHOULD MAY MAYBE-NOT DONT matrix (in pseudo-normative form) I used all but one of the MUSTS. I used a few of the SHOULD I used a similar few of the MAY I only used one of the MAYBE-NOT (pow, and I am doing base arithmetic but not crypto) I don't think I use any of the DONT I'm interested in moving up on the SHOULD and MAY
- laurentoget 7y agoi cannot remember the last time i used str() instead of a string format.
- ausbah 7y agoI am honestly a bit surprised divmod and complex are so high, I have only used them a couple of times myself - but I think they're almost always mentioned in "Intro to Python" books so most people should at least be familiar with them right?
- kabacha 7y agoI 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.
- ehsankia 7y agoI 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.
- abhishekjha 7y agoForgive me doesn’t deque mean double ended queue? Also why can’t a simple array behave like a list?
- tehnub 7y agoYes it does mean double ended queue. I'll assume you mean a python list when you say simple array. One drawback of lists is that you cannot add a new element to the start of a list in constant time (with a python list it requires O(n) time), whereas you can with a deque.
- nathanasmith 7y agoI had a script a while back that created really long lists at runtime by continually appending data as it came over the wire. The lists would quickly get so long that I needed to remove items from the beginning to conserve memory though because reasons I could only remove items one at a time. Long story short, converting those lists to collections.deque and making use of popleft() rather than "del l[0]" improved performance considerably.
- 7y ago
- chombier 7y agoFor a few moments I thought of all these years during which I could have just typed "breakpoint" instead of "import pdb; pdb.set_trace()". Luckily it's been added only recently (3.7) :)
- edanm 7y agoWow I had the same reaction. And that's despite the fact that I read an article a few weeks ago about this new feature! If you haven't read it, I highly recommend it, because there's more to the breakpoint feature than mentioned here (basically, you can set an env variable to choose which callable is activated on breakpoint, meaning you can turn on and off debugging on production systems, or even set prod systems to open a web port for debugging): https://hackernoon.com/python-3-7s-new-builtin-breakpoint-a-quick-tour-4f1aebc444c https://hackernoon.com/python-3-7s-new-builtin-breakpoint-a-...
- tigrezno 7y agoStop using python, let it die for god's sake!
- amelius 7y agoI miss the "map" function. Sadly it was removed in the transition from Python2 to Python3. In Python3, "map" is a class.
- radarsat1 7y agoI notice that "globals" is on the "you won't use this much" pile. I am wondering, when using Python interactively from Emacs, I often do this kind of thing: if 'myvar' not in globals(): myvar = ... That way I can just execute the whole buffer instead of having to carefully be picky-choosy about what I send to the interpreter. It's especially helpful if the line involves loading a large file, calculating something that takes a while, or downloading something. Is there a better way to handle this in an interactive context? Like an automatic globals cache of some kind? (Note, it's helpful to be able to dirty the variable when necessary by simply, del myvar )
- MattConfluence 7y agoA recent(-ish) addition to Python 3 I also think is worth mentioning is the statistics module: https://docs.python.org/3/library/statistics.html https://docs.python.org/3/library/statistics.html Every codebase eventually needs an averaging function, now you don't have to re-implement it yourself every time. Plus it's a nice toolbox of a few different utilities that you would otherwise need to install numpy/scipy/etc. to get.