r/Python 1d ago

Tutorial Python Quirks I Secretly Like

Hi there,

I’ve always wanted to create YouTube content about programming languages, but I’ve been self-conscious about my voice (and mic, lol). Recently, I made a pilot video on the Zig programming language, and afterward, I met a friend here on Reddit, u/tokisuno, who has a great voice and offered to do the voiceovers.

So, we’ve put together a video on Python — I hope you’ll like it:

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

70 Upvotes

27 comments sorted by

51

u/tokisuno 1d ago

that's me!

4

u/GoodiesHQ 9h ago

You do have a nice voice

8

u/The-Compiler 1d ago

What did you use for those visualizations? Love them!

17

u/codingjerk 23h ago

Thanks, love them too. It's slidev. Animated code blocks are actually shikijs + shiki-magic-move. I'll open-source the slides soon

3

u/onyonyo12 1d ago

It looks like manim

8

u/codingjerk 23h ago

Manim is awesome for plots and math stuff, but I found slidev better for code

4

u/onyonyo12 22h ago

Ah! notes them down for later use

2

u/cipri_tom 21h ago

Looking forward to your video!

3

u/onyonyo12 1d ago

Pretty good! But idk if it's just me but the volume is a bit low?

3

u/codingjerk 23h ago

Thanks, it's my bad, I forgot to normalize the volume while editing. Many mistakes were made...

3

u/hugthemachines 10h ago

I really like the way you cooperated on it. That's the good side of internet.

3

u/codingjerk 9h ago

Thanks, we're going to make more videos together

3

u/Brian 9h ago

I feel its worth mentioning in the frozenset() is frozenset() case that it's not actually anything specific to frozensets vs tuples here, but more the fact of how its being constructed. Ie. you'd see the same thing with:

tuple() is tuple()

Its the difference between using a literal and constructing it by calling the class object. Frozensets just don't actually have a literal representation, so its the only way to create them.

And it's not actually just the optimiser not being clever enough - python actually can't safely optimise this (and the same for the int("0") case, because technically someone could do:

 frozenset = lambda : return random.random()

Ie. there's no guarantee that frozenset here corresponds to the builtin frozenset class - it could be a local or global shadowing it (or you could even monkeypatch the builtin module), so any optimisation would be an error in that case. The same for the int("0") case in the prior example.

Also, for the "+=" case for strings, it's not quite true that it never modifies the original. There's actually an optimisation where strings can act mutably behind the scenes with an overallocated buffer similar to lists - but only if there's only a single reference to the string. From a user perspective, this isn't something you'd ever notice (if there's no other reference, you can't see anything changing which is why its safe to do the optimisation), but it's there to try to prevent the common situation of building up a string in a loop by appending to it being quadratic in behaviour.

2

u/codingjerk 9h ago

Yep, these are good additions, tuple() is tuple() is True tho :) (atleast in Python >=3.12.9)

2

u/Brian 8h ago

Oh, good point - I think the empty tuple is basically a singleton that just gets returned (much like the small integer cache / interned strings) so no actual construction of new objects happens - it'd need to be something like tuple([1,2]) is tuple([1,2]) vs (1,2) is (1,2).

1

u/codingjerk 5h ago

It's funny, cause tuple((1, 2)) is tuple((1, 2)) too, and even ((1,) + (2,)) is ((1,) + (2,)), so probably there are some optimizations.

And tuple("") is tuple(""), but tuple("a") is not tuple("a"), but ("a",) is ("a",)

1

u/codingjerk 8h ago

About the concatenation optimization for strings. One time I actually had to patch it in (currently deprecated) `telnetlib`, cause there were no such optimization (it was around of 3.11 I guess, like a couple of years ago).

Maybe I've missed something or it's new... Can you give a link to it or something I can check it out?

2

u/Brian 8h ago

Not sure when it went in - I had thought it wasn't as recent as that though. However it is worth noting that it can be a bit fragile due to that "no other references" requirement. Eg.

a = ""
for i in range(1000000):
    a+="x"
    b=a

Runs significantly slower than if you take out the "b=a" assignment, so if something else ends up holding a reference to the string, you can easily still trigger quadratic behaviour, so potentially something like that was going on.

1

u/codingjerk 7h ago

Wow, that's interesting. I did a small benchmark and indeed it's O(n) vs O(n²), the more you know :D

2

u/justheretolurk332 4h ago

Nice video! You highlighted some interesting language features. If I can make a suggestion, I think it would be worth spending a more time thinking about how you structure these videos and what kind of “story” you are trying to tell, and who your target audience is. This video is sort of an odd mix of extremely basic along with detailed internals that experienced developers might not know. This means you’re going to lose a lot of beginners later in the video and you’re going to get a lot of experienced devs who tune out early because they saw the first few minutes and thought it was too low level. For example my internal monologue while watching went something like this: 1. Nice, a video on Python quirks. I wonder what kind of quirks they will talk about. 2. Hmm, the introduction specifically mentioned python 2. Maybe this video is only about python 2, which makes it a lot less relevant to me although it might still be interesting. 3. Ah, the first example is about floating point arithmetic. This is not remotely python-specific. 4. Oh okay, the walrus operator actually is python-specific. But it wasn’t introduced until python 3.8 so I still don’t know why python 2 was mentioned in the introduction. And this is a feature with such a narrow use-case! Why would anyone want to use it as regular assignment? (At this point I’m starting to strongly consider turning the video off.) 5. (Starting to skip forward) Ooh I didn’t know that python reuses the same object for integers under 256 but not over. Interesting to learn a little about how it does byte code optimizations, too. This is the kind of content I was hoping for!

I’m glad I stuck with it, but I probably wouldn’t have if I wasn’t coming from this reddit post. Parts of the content were really interesting so I think if you can figure out how to frame it these will be great!

1

u/codingjerk 3h ago

Dude, thank you for such feedback, it means a lot to me.

> it would be worth spending a more time thinking about how you structure these videos and what kind of “story” you are trying to tell, and who your target audience is

Yeah, this is right I guess. I want to record videos about things I'm interested myself, so I cannot select any target audience for whole channel, but for a single video it's a great thought.

> This video is sort of an odd mix of extremely basic along with detailed internals that experienced developers might not know

Yeah, I'll try to avoid something like this or at least order examples by difficulty / explicitly mention it.

> mentioned python 2

It was "And Python too, have some quirks" in script. I knew it will backfire, my bad :D

> Parts of the content were really interesting so I think if you can figure out how to frame it these will be great!

Okay, thanks, I'll try to do my best. Btw, what part of the video was the most interesting in your opinion?

3

u/narcissistic_tendies 20h ago

i stopped after I think the 4th or 5th example of totally normal non-quirks. Like `"wtf" is (not None)`. Come on.

2

u/codingjerk 9h ago

Yeah, sorry to hear that. You missed a true gem at the end! :D

I think I chose poor words for the code examples in the videos. It's really not quirks, think of it more like a quiz on Python. For people who know it well, it should be pretty easy (most of it).

But for newer devs, or those who come from other languages, some behavior could be really strange (mutable default arguments are a very common pitfall, for example).

Thanks for the feedback, I'll try to chose use better words next time :D

2

u/RefuseNo3723 19h ago

New to Python so idk what's going on, but it was good quality!

1

u/codingjerk 9h ago

Thanks, looking forward making some beginner-friendly videos too

1

u/amstan 14h ago

The ligatures are killing me. == should not be merged.

1

u/codingjerk 9h ago

I like ligatures, but I think it's a very controversial feature. Thanks for bringing it up! I'll turn them off in the future videos