Replies

This profile is from a federated server and may be incomplete. Browse more on the original instance.

treyhunner, to python
@treyhunner@mastodon.social avatar

Knowing when to use a set in can be VERY handy sometimes.

no_neighbors = [
n
for n in nums
if n-1 not in nums
and n+1 not in nums
]

If "nums" is a list of 500,000 numbers, this takes 54 minutes.

Convert "nums" to a set and this taken less than 1 second.

Why?

It's all about that "in" operator.

Checking for containment in a list requires looping over the whole list.

Checking for containment in a set uses the magic of hashing to return an answer without looping.

treyhunner,
@treyhunner@mastodon.social avatar

Our looping is compounding here, which makes things even worse (loops inside of loops are where things can get really slow).

The set case does ~500 thousand operations.

The list case does ~500 billion operations.

In computer science terms:

"x in my_list" has a linear time complexity: O(n)

"x in my_set" has a constant time complexity: O(1)

@nedbat has a great article/talk on "Big O" in the context of :
https://nedbatchelder.com/text/bigo.html

treyhunner,
@treyhunner@mastodon.social avatar

If you need to see this to believe it, try running this code which does the same comparison between 50,000 items (which is much faster but still takes nearly a minute for the list scenario.

This will run each scenario & print how fast each took.

https://pym.dev/p/2aayq/

treyhunner,
@treyhunner@mastodon.social avatar

This thread is a preview of a premium @PythonMorsels screencast on list containment checks & why they're slow: https://pym.dev/list-containment-checks/

In Python Morsels screencasts, I distill complex topics down to just a few minutes of explanation. 🧠 ⏲

If that sounds interesting, you can here: https://www.pythonmorsels.com/watch-python-screencasts/

treyhunner, to python
@treyhunner@mastodon.social avatar

Ever seen a variable with two underscores on either side (like_this)?

These variables define a contract between Python devs and the interpreter.

Officially called special attributes, they're often called "dunder variables" colloquially.

Each dunder variable is typically used for one of two purposes:

  1. Conveing information (metadata) to Python devs
  2. Empowering Python devs to convey information to the Python interpreter

name is an example of 1.

repr is an example of 2.

treyhunner,
@treyhunner@mastodon.social avatar

So in general each dunder variable is either meant to be READ by programmers or WRITTEN by Python programmers... but not both!

The name method in a module is meant to be read but not written.

But dunder methods are meant to be written, but not read.

More on dunder variables in this article 🔖
https://pym.dev/dunder-variables/

treyhunner,
@treyhunner@mastodon.social avatar

@daniellindsley I categorize that under 2, but you're right that overloading of operators & built-in functions in particular is quite novel when mixing from many languages.

treyhunner,
@treyhunner@mastodon.social avatar

@folkerschamel if you're defining a dunder method you're allowed to call the same dunder on a parent.

But if you're doing that from outside a init definition, something very weird is going on! 😜

treyhunner,
@treyhunner@mastodon.social avatar

@chrysn Absolutely.

Great counter-example. 👍

I'd argue that "more of a guideline than a law" applies to pretty much every Python maxim... including the whole Zen of Python! 😅

webology, to random
@webology@mastodon.social avatar

🏕️ "Leave it better than when you found it."

🤔 I am sure it will take me a while to update my various email and social media footers, but my ~5-year term as @ThePSF director and vice chair ended today.

🙏 It was an honor to serve, work with everyone, and leave a small mark on the community and our direction.

👟 If you are a runner and have ever finished a race a little faster than you thought you could or met a new distance goal and surprised yourself, that's how I feel today. ❤️

treyhunner,
@treyhunner@mastodon.social avatar

@webology @ThePSF I'm really glad the PSF board had you on it for as long as it did! ☺️ Thank you! 💖

treyhunner, (edited ) to python
@treyhunner@mastodon.social avatar

In Python we tend to use ALL_CAPS for constant variables.

Keep in mind that this is just a convention though. Python doesn't actually have constants.

Python DOES have immutable objects, but those are different from constants.

Remember, has 2 types of change: mutations change objects and assignments change variables. A true constant variable (not Python's) cannot be re-assigned while an immutable object cannot be mutated.

https://m.youtube.com/watch?v=0yrlE_XCMEE&feature=youtu.be

treyhunner,
@treyhunner@mastodon.social avatar

@spencer @diazona when describing the nature of Python's variables, I usually use the terms "point" and "pointer", though reference and bind seem like fine terms (though less visually obvious to beginners).

https://pym.dev/pointers

treyhunner,
@treyhunner@mastodon.social avatar

@_KevinTrainor in the last paragraph I was speaking of true constants (contrasting them with immutable objects), not python's constant convention.

wsvincent, to random
@wsvincent@fosstodon.org avatar

I wonder if there's room for something between short tutorials and courses. Maybe "recipes" where I just show how to do common intermediate-ish Django tasks, but don't have to explain all the steps the way I normally do? So not docs that are abstract, still step-by-step, but more like here's the recipe and there are links to go deeper on the "why" if you need it.

treyhunner,
@treyhunner@mastodon.social avatar

@CodenameTim I'm planning to eventually integrate screencasts and mini exercises, which might fit the dream that @wsvincent's has even closer.

I do think this is sorely needed for Django! 💗

treyhunner, to random
@treyhunner@mastodon.social avatar

New screencast: How to make a context manager 📺🐍🎛

I've taught this topic for years, but this is the first time I've whittled this all down to just 5 minutes.

https://pym.dev/creating-a-context-manager/

treyhunner,
@treyhunner@mastodon.social avatar

@siddhantgoel agreed! Hoping to do a separate screencast on that.

treyhunner, to python
@treyhunner@mastodon.social avatar

What feature would you have trouble explaining to a new programmer?

(If you think you CAN explain that feature, feel free to reply to replies with your own new-programmer-oriented explanation)

treyhunner,
@treyhunner@mastodon.social avatar

@kleaders It may seem odd to copy-paste your way from a loop, but it's not meant to be a permanent technique.

The idea is to anchor what you're less familiar with (the comprehension) on the syntax of what you know (the loop).

The syntax is weird! It uses the keywords "for" and "if", which makes it look like an inside-out "for" loop, even though it's really something separate.

More:
• Short: https://pym.dev/turning-loop-list-comprehension/
• Longer: https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
• Longer-er: https://trey.io/comprehensions

treyhunner,
@treyhunner@mastodon.social avatar

@kleaders you're welcome!

treyhunner,
@treyhunner@mastodon.social avatar

@kleaders @meejah big +1 to breaking over multiple lines. I pretty much always prefer that.

Something to note is that unfortunately using the Black autoformatter will turn them back to one-liners unless they're over the maximum like length you have set. 😢

  • All
  • Subscribed
  • Moderated
  • Favorites
  • anitta
  • thenastyranch
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • ngwrru68w68
  • megavids
  • magazineikmin
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • provamag3
  • tester
  • Leos
  • JUstTest
  • All magazines