I feel like WTFs caused by doing something deeply weird hardly counts against the language. Yes doing the walrus operator inside brackets works. Yes of course it's going to be doing weird things. Is this really surprising to anyone? It's not what that operator is for . Yes comparing strings with "is" works sometimes . It's not checking equality and it isn't supposed to. In another implementation using "is" might work…
>> none of the code on this page is something anyone would actually write in production code on purpose. I mostly agree, but there is one example where I thought it is important to know and remember: for x in range(7): def some_func(): return x Here x binds to the (outside) symbol x, not the value of x at creation time of the inner function.
WTF Python: Exploring and understanding Python through surprising snippets
71–80 of 169 posts
Re: WTF Python: Exploring and understanding Python through surprising snippets
#72Earlier quoted context omitted.
You’re right - I’m not suggesting that `+` should work in-place, but that `a += b` should do the same thing as `a = a + b` and not work in-place.
So you're saying that a += b should create a new object and assign it to a? Now that would be even more unexpected...
It’s even worse to do the first for some types and the second for other types.
Re: WTF Python: Exploring and understanding Python through surprising snippets
#73Earlier quoted context omitted.
Can you imagine same "yes - ..." comments made about some new, straight out of the oven language? Most people would not touch it with a ten foot pole.
And it'd be unfair to that new language too. Basically all of the WTFs in the OP are things you don't run into, unless you're writing deeply deeply weird code, at which point it's hardly the languages fault that it does weird things.
Re: WTF Python: Exploring and understanding Python through surprising snippets
#74To be frank I think the python community lacks self-awareness. There's this ritual you have to do when learning python: blindly accept status-quo design choices as "universally good" (but call it "pythonic"), and crap on other languages. Never admit python or the ecosystem has issues or shortcomings, and if another language has something better than yours (eg, Yarn, or Composer is way better than anything in Python (…
PHP did well with v5 to v7. It took more than a year but your point is well made.
The PHP v7 to v8 move is not as easy [1, 2, 3]. Not having Python 2 to 3 fresh in everyone's minds is one reason; not having failed PHP v6 is another. The breakages that led to the downfall of PHP6 made backwards compatibility a focus in PHP7.
Since PHP7 was released the contributors/voices who favour backwards compatibility have left or been drowned out. The emphasis on supporting legacy code has gone.
[1] https://stitcher.io/blog/new-in-php-8#breaking-changes
[2] https://24daysindecember.net/2020/12/21/a-perfect-storm/ (recommended reading)
[3] https://developer.yoast.com/blog/the-2020-wordpress-and-php-...
Re: WTF Python: Exploring and understanding Python through surprising snippets
#75To be frank I think the python community lacks self-awareness. There's this ritual you have to do when learning python: blindly accept status-quo design choices as "universally good" (but call it "pythonic"), and crap on other languages. Never admit python or the ecosystem has issues or shortcomings, and if another language has something better than yours (eg, Yarn, or Composer is way better than anything in Python (…
Python: where relying on “lol magic” is acceptable thing to have in production code. At least “magic” in languages like Haskell has the dignity to be backed up by a type system with more consistency than a wet noodle. Python can’t seem to decide whether it wants to be a production grade language or easy-for-beginners and keeps implementing pointless features (looking at you walrus operator and weird version or patter…
I have been burnt by this. As much as I like python's "expressiveness" so to speak, it is too slow for my needs; the ML community gravitated towards that because it is simple to implement and test large models because C++/C/Cuda are doing the lifting. However, in my flavour/field of ML, python is often the bottleneck because the sampling/simulations are glued with it and the need to sample 10s of millions of steps ends up with possibly billions of function calls in python.
Re: WTF Python: Exploring and understanding Python through surprising snippets
#76To be frank I think the python community lacks self-awareness. There's this ritual you have to do when learning python: blindly accept status-quo design choices as "universally good" (but call it "pythonic"), and crap on other languages. Never admit python or the ecosystem has issues or shortcomings, and if another language has something better than yours (eg, Yarn, or Composer is way better than anything in Python (…
> Why do my lines of code need to fit on a punched card invented decades before the language was invented?
Keeping lines short helps keep information density high, allows you to fit more views of the code on screen without wrapping, and encourages use of intermediary variables to keep complexity per line low.
This isn't a Python issue. Any decent style guide will have a maximum line length.
> Python is a fractal of closed-mindedness
I don't know what you mean by a "fractal" of closed-mindedness, but you seem very certain of your opinions and not very open-minded.
Re: WTF Python: Exploring and understanding Python through surprising snippets
#77Earlier quoted context omitted.
I agree with most of your points, because in most cases, people try something very weird, and they are surprised that the result is something even weirder (or they just didn't think through the example logically). Except the string comparison with "is". It's confusing, because there are no errors, and when I try them in the Python REPL, it "works". The code is logical, I tested the example and it works... ...except w…
The confusion there is caused by english not disambiguating between equality and being-the-exact-same-thing-ness. I'll grant that "is" is a bit of a footgun for new users, but it's not a WTF in the sense that the language is doing something weird. Rule of thumb is use == for everything except True/False/None. Once you get going with python you'll learn the meaning of is eventually, and you'll still barely ever need t…
Re: WTF Python: Exploring and understanding Python through surprising snippets
#78The only one I actually encountered myself is `def some_func(default_arg=[]):` one, but it was indeed a very "WTF" moment.
Re: WTF Python: Exploring and understanding Python through surprising snippets
#79To be frank I think the python community lacks self-awareness. There's this ritual you have to do when learning python: blindly accept status-quo design choices as "universally good" (but call it "pythonic"), and crap on other languages. Never admit python or the ecosystem has issues or shortcomings, and if another language has something better than yours (eg, Yarn, or Composer is way better than anything in Python (…
Re: WTF Python: Exploring and understanding Python through surprising snippets
#80Ah yes, the one about mutable default arguments was a real surprise for me once in a debugging session. Python only initialising them once during the whole script runtime is not exactly the behaviour anyone would expect.
They're also an escape hatch, def main(): funcs = [] for x in range(10) def f(x=x): return x funcs.append(f) print([f(x) for f in funcs]) The "x=x" default argument is necessary here. In this example it's just a constant but this trick is often used with mutable defaults.
[1] https://github.com/satwikkansal/wtfpython#-beware-of-default...
[2] https://github.com/satwikkansal/wtfpython#-loop-variables-le...
> In this example it's just a constant but this trick is often used with mutable defaults.
Your example doesn't work with mutable defaults. Ironically it falls into the trap the parent comment was referring to!
def main():
funcs = []
arg = []
for x in range(10)
arg.append(x)
def f(arg=arg):
return arg
funcs.append(f)
print(*[f() for f in funcs], sep="\n")
# result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] printed 10 times
These functions will all return the same 10-element list. Instead you need to use `def f(arg=arg[:])` or `def f(arg=copy(arg))` (after from copy import copy).> print([f(x) for f in funcs])
Just a typo but you meant print([f() for f in funcs])