treyhunner,
@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)

danjac,
@danjac@masto.ai avatar

@treyhunner the new ExceptionGroup, because I haven't been able to wrap my own head around it yet...

carlton,
@carlton@fosstodon.org avatar

@treyhunner for...else — utter madness 😜

jrocket_dev,

@treyhunner I’ve always found teaching context managers to be difficult… both why they are useful, how to use them effectively, why some things are context managers and some aren’t (and some work as both), and how and when to build your own.

technige,
@technige@fosstodon.org avatar

@treyhunner The difference between tuples and lists. The use cases are clear in my head, but you need a minimal amount of programming context before they stop looking like interchangeable structures.

adamchainz,
@adamchainz@fosstodon.org avatar

@treyhunner match/case… I just haven’t found uses for it beyond the simplest comparisons.

treyhunner,
@treyhunner@mastodon.social avatar

@adamchainz good one! 😅 I have found some non-trivial uses for match/case but there are some corners that I would really struggle to explain. 😬

satya,
@satya@mas.to avatar

@treyhunner @adamchainz I love match..case, but I have to look up the tutorial anytime I try to do something interesting. Definitely don’t feel up to explaining it.

kleaders,
@kleaders@fosstodon.org avatar

@treyhunner I still have trouble with list comprehensions myself. They are very powerful, but the syntax always confuses me.

treyhunner,
@treyhunner@mastodon.social avatar

@kleaders great one!

Comprehensions are one of those features where throwing a few explanations at the wall can be helpful because different mental models work better for different folks.

Here's one explanation of comprehensions:

The original comprehensions from other languages come from set builder notation in math.

[ x*2 | x <- numbers, x%2==1 ]

But we don't like symbols in so we instead write that as:

[x*2 for x in numbers if x%2 == 1]

treyhunner,
@treyhunner@mastodon.social avatar

@kleaders here's another explanation of 's comprehensions.

If you have a "for" loop that consist solely of an append, an optional "if" that the append is within, and maybe a nested "for" loop in your loop, you can copy-paste your way into a comprehension.

The expression you're appending comes first. Every other expression remains in the same order.

I recommend folks start their comprehension journey by writing the "for" loop first (since it's familiar) and then copy-pasting into it.

kleaders,
@kleaders@fosstodon.org avatar

@treyhunner thank you!

treyhunner,
@treyhunner@mastodon.social avatar

@kleaders you're welcome!

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

meejah,
@meejah@mastodon.social avatar

@treyhunner @kleaders Indenting can help a lot too; I like to keep three lines in comprehension: the first is the expression, second is the "for" and lastly is any "if". So like this (hoping the whitespace etc stays intact):

doubled_odds = [
n * 2
for n in numbers
if n % 2 == 1
]

meejah,
@meejah@mastodon.social avatar

@treyhunner @kleaders Also, if it gets much longer or more complex than the above I prefer the "unrolled" version for future-me's reading clarity.

kleaders,
@kleaders@fosstodon.org avatar

@meejah @treyhunner thanks, this is helpful!

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. 😢

meejah,
@meejah@mastodon.social avatar

@treyhunner @kleaders Definitely one of the things I don't like about black ;)

orsinium,
@orsinium@fosstodon.org avatar

@treyhunner Specifying and passing arguments for functions in is incredibly more complicated than it sounds and I haven't seen it in any other language I know. You have optional, positional, positional-only, keyword, and keyword-only arguments, default values (and don't forget the gotcha with mutable defaults), type annotations, *args, **kwargs, two kinds of unpacking (iterables and mappings), magic binding of self, and now also type parameters. I'm out of characters just listing it.

treyhunner,
@treyhunner@mastodon.social avatar

@orsinium yup. There is more to arguments than is worth learning/teaching in an introductory course. I always include a preview of some of the more complex stuff and a disclaimer that you don't need to know most of it to get started and you shouldn't try to memorize most of it beyond positional vs keyword and defaults (until you find yourself needing it...).

gwidion,
@gwidion@floss.social avatar

@treyhunner typing.
also, to a seasoned programmer.
And I'd take sometime to explain "you don't need this, you are just working for free for the tools" for programmers coming from static typed languages.

siddhantgoel,
@siddhantgoel@mastodon.social avatar

@treyhunner async/await would be very high up on that list for me.

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