13 Python Quirks That Will Surprise You

preview_player
Показать описание

In this video, I’ll show you 13 things in Python that are just weird. The last one is really confusing, especially if you’re a new Python developer. I’ll go through each of these with a code example and I’ll explain why it’s like that.

🔖 Chapters:
0:00 Intro
0:12 #1 Integer caching
1:46 #2 Default mutable arguments
3:44 #3 NotImplemented VS NotImplementedError
7:24 #4 Immutable objects
9:08 #5 Booleans are weird integers
11:07 #6 Reference inconsistency
13:02 #7 Variable scope weirdness
14:30 #8 For-else clause
16:03 #9 Delayed printing
17:03 #10 JavaScript in Python?
19:08 #11 Space invader operator?!
20:08 #12 Functions have attributes?
21:05 #13 Class or instance attributes?
25:33 BOUNS

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

I hope you guys are not superstitious 😁.

ArjanCodes
Автор

I never paid attention to the print flush behavior before, it makes sense now that I think about it. Thanks for pointing that out!

mCoding
Автор

That last section was very hard to follow with the switching between windows and the high number of things being printed. Comments next to the lines with the results would have helped a lot, or maybe using a jupyter notebook could have worked as well.

jeffbeasley
Автор

In C, false is zero and everything else is true. And that's exactly the way while loop conditions work in Python as well. You can put a number and when it's not 0, it's true and the loop continues.

marcotroster
Автор

Why not use f-strings to show the evaluated expression as well as the result to save flipping between screens?
E.g.
>>> print(f"{(1 == True) = }")
(1 == True) = True

michaelthomson
Автор

My other favorite easter egg in Python - in any REPL, type "from this import that" - it prints the zen of Python poem. But if you go look in the module, the string is encrypted with a substitution cipher!

Side note, if you use ctrl+alt+up/down, you can do multiline cursors in VS Code (similar to ctrl + click). So if you have a bunch of statements that line up in a columnar way you can edit them all at the same time. PyCharm has similar functionality but you have to press ctrl 2x in quick succession and hold it down on the second press, then use the arrow keys, which I like a lot less.

alberthalbert
Автор

Many thanks for your very instructive videos.
Wanted to ask you what font you are using in the video.

diegomerino
Автор

What version of python do I need to use, or what should I import, for your function signature in Ex#2 to work?

def append_to_list[T]... 3.11.4 doesn't like the "[T]"

midinerd
Автор

Great stuff! I did not hear about NotImpemented (and, obviously, antigravity 😂) before this video. I only think that while most of the other features are caused by the peculiarities of the language implementation, item 6 is not truly weird, as it is what anyone might expect from string immutability and variable reassignment

Oddys
Автор

Hi Arjan, again a very nice video, so thanks for that!

With respect to the function attributes: yes at first it may seem strange that such an attribute definition on a function is possible. On the other hand, if you look at the output of dir(functionName) you can see automatically defined attributes, all of them being dunder-attributes (starting and ending with double underscores).
Example is that a function documented with a docstring has that documentation stored in the attribute __doc__.
Knowing that dunder-attributes are usually defined by Python itself, it makes sense that non-dunder attributes are available to the programmer. I agree with you that it is not common use to do.
As mentioned by another reader, using decorators on functions is a more widely used practice to obtain functionality that could otherwise be reached using function attributes.

jurgenrusch
Автор

About "weirdness" of class and instance variable: it's confusing only when you don't know the Python language. It's called MRO and well described in the official documentation.

danilshein
Автор

Function attributes are cursed but can also be pretty awesome. Being able to count invocations, automatically log calls, etc. You can also usually accomplish the same with decorators though

kevinryan
Автор

Not sure if this classifies as weirdness, if anything it is perhaps rather unexpected but in a good way: In python almost anything is fair game. Asigning attributes that were not defined on the class? Sure! Swap out methods using moneky-patching!? Go ahead! Hooking into the import system to allow loading modules directly from pip? Why not. But if you try to return a negative number from `__len__` the interpreter will scream at you =D

XRay
Автор

8:47 data[0] = data[0] + [3] does NOT give the same result as data[0] += [3]

marckiezeender
Автор

Have you considered using the multi cursor to avoid wasting video time just changing repetitive things line by line

Love your videos!

aghnos
Автор

Before I followed this channel I never actually used dataclasses so it wasn’t such a big deal to me. And to make sure I don’t get confused I always use all caps to define class attributes. But then I found this channel and I soon realized how convenient data classes. Now I’m starting to get confused….😅

CalvinJKu
Автор

Arjan, do you think you could tell viewers at the beginning of a video which version of Python you were using? It's frustrating to type in your examples only to have them crash because of version issues.

Impatient_Ape
Автор

Function name starts with double underscore inside a class can cause big trouble for child class. It kind of makes it an exclusive method for the super class.

macong
Автор

This is nice, speaking as someone who does far more JavaScript than Python, it's nice to see Python taking some of the abuse that's normally thrown at JavaScript. ;)

edgeeffect
Автор

9 has nothing to do with Python, it's just how OS works. The output is line-buffered by default, so OS won't pass it on to the terminal before it contains \n (which you purposefully suppressed) or the program exits.

OS provides a way to force buffer flush (which is what `flush=True` does) or to switch to other buffer strategy (eg. by buffer size).

AloisMahdal