by softwaredoug · 3y ago · 0 comments
I asked ChatGPT:
> Gary Berhnhardt gave a famous talk called "Wat" about Javascript. Can you create similar "Wat" style examples for Python?
The examples it gave me were... not that 'wat'-y
Sure, here are some "Wat"-style examples for Python:
1. Unpacking tuples:
>>> a, b = (1, 2)
>>> a
1
>>> b
2
>>> a, b = 1, 2
>>> a
1
>>> b
2
2. Division by zero: >>> 1 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
Though this one is interesting:4. Using the is operator:
>>> x = 256
>>> y = 256
>>> x is y
True
>>> x = 257
>>> y = 257
>>> x is y
False"Mutating the immutable" is my personal favourite - the second line of code fails with an exception despite having already succeeded:
>>> t = ([1, 2], 5, 6)
>>> t[0] += [3, 4]
TypeError: 'tuple' object does not support item assignment
>>> print(t)
([1, 2, 3, 4], 5, 6)
I've never been a fan of the decision to have `a += b` sometimes do `a = a + b` and sometimes update `a` in place. My objection was initially just due to theoretical inelegance - but this code shows that decision directly causing a concrete problem.Python does not coerce types, so it doesn't have very many wats like the ones in this talk.
<dis>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
Some more weird/fun stuff about that: If you throw that code into one file instead of a repl, `x is 257` will be True. If you instead instantiate `x = 257` in a different file, and import it, `x is 257` be False again. In a repl if you run them on the same line it ends up as True! >>> x = 257
>>> print(x is 257)
False
>>> y = 257; print(y is 257)
True
If they're in the same file (or same line in a repl), the integer literals are loaded into the `co_consts` object during the initial parse to bytecode, and so they refer to the same object. This only works for integer literals and simple statements that can be optimized to literals by python (e.g. it also works for `x = 257+1; print(x is 258)` >>> dis.dis('y = 257+1; print(y is 258)')
1 0 LOAD_CONST 0 (258)
2 STORE_NAME 0 (y)
4 LOAD_NAME 1 (print)
6 LOAD_NAME 0 (y)
8 LOAD_CONST 0 (258)
10 IS_OP 0
12 CALL_FUNCTION 1
14 POP_TOP
16 LOAD_CONST 1 (None)
18 RETURN_VALUE > True = False
> if not True: print “can’t happen”
can’t happen of = "wat";
for (of of of) of -1/3 == -(1/3)
Where I would ask people what it evaluates to, and give them the choices: "True", "False", or "Python 2 or Python 3?" def add_to_list(value, target_list=[]):
target_list.append(value)
return target_list
list1 = add_to_list(1)
list2 = add_to_list(2)
print(list1) # [1, 2]
print(list2) # [1, 2]
In this example, one might expect list1 to contain only 1 and list2 to contain only 2. However, because the default value for the target_list parameter is mutable, it gets shared between function calls, leading to unexpected behavior.
The other examples it gave me were not that unexpected. Prompt was:> Show me examples of where Python type inference works in unexpected ways.
bit of a weird way to phrase it, I think they're always shared between function calls, you just don't notice when they're not mutable.
print(list1) # [1, 2]
print(list2) # [1, 2]
Some would expect it to be: print(list1) # [1]
print(list2) # [2]
The reason it's weird is because Python evaluates default arguments at function definition time rather that function call. This means that it's unsafe to use mutable values as default values in params. See the equivalent in JavaScript: function addToList(value, targetList=[]) {
targetList.push(value);
return targetList;
}
const list1 = addToList(1);
const list2 = addToList(2);
console.log(list1); // [1]
console.log(list2); // [2]A pretty funny kind of bug, actually: it was some relatively simple function, passing the unit tests but doing some nonsense on real data. Thing is, it was comparing some sequential ids and working perfectly as long as there were no more than 256 objects.
(Didn’t read the rest, but based on the title it may also be relevant)