https://www.oreilly.com/library/view/javascript-the-good/978...
Helped me get a more pragmatic approach to use that chaotic mess of a language and environment. Just use what works, ignore the rest (but I also ignored some specific advice from the book and used what worked for me).
Every single new, large feature in JS has been full of quirks. The same cannot be said for Python; the exceptions are few and far between and more often than not they're not actually exceptions but rather something bound to core language fundamentals which once understood don't present a challenge because there is an actual underlying consistency.
(Note some of these are outdated, but some of those only mention that towards the end of the section rather than the beginning)
In about the same timeline, Py went from threading to async-await, which created classic blocking vs nonblocking mismatches. The whole 2 to 3 breaking migration was also still a big deal in 2015.
I challenge you to find Python behaviors as weird and off-putting as anything here: https://wtfjs.com/
Python has weird file imports (no relative ones either), broken package management, historical differences between asyncio and blocking that still cause issues, threading/GIL caveats that trip up even experienced users, indentation for scope (esp weird given it was designed for REPL), weirdly no anonymous functions, 2 vs 3 (mostly gone by now), the __init__ and __init__.py stuff, namedtuple vs dict vs object, `global`, and a whole mess with type-linting if you're going there. You have to deal with all those things every time.
Here's one little Python footgun that everyone hits and is also annoying after:
def func(array=[]): # default value is empty array, right?
array.append("asdf")
print(array)
>>> func()
['asdf']
>>> func()
['asdf', 'asdf']This is so much so that it is described in the Zen of Python: There's preferably One Way to do things, but that one way may not be obvious.
A gun becomes a footgun when you put it in the hands of someone who doesn’t understand how it works and they promptly blow off their foot.
I don't know why you think it should be so common, because mutating a parameter with a default value doesn't make a lot of sense in the first place. But also it's one of the single most well-documented things about the language, and it's also historically been found useful when invoked intentionally (https://stackoverflow.com/questions/9158294).
Also, we call that a list, not an array.
> no relative ones either
It certainly does have relative imports, and I have no idea what you think is "weird" about the import system.
If you're talking about dynamically importing from a string path, that, too, works just fine with a relative path. It's just that relative paths are relative to CWD rather than the source file, just like any other time you open a file.
If you're annoyed that you don't just specify a file path, keep in mind that a Python module doesn't have to correspond to a file at all. What you ask for in this case is impossible and nonsensical.
> weirdly no anonymous functions
Incorrect. `lambda`.
> differences between asyncio and blocking
This is like saying "differences between cars and traffic jams". It makes no sense whatsoever.
I could go on, but the short version is that you do not know what you are talking about.
Python has "relative imports" in that you can import relative packages, not files. So the weirdness is, even within my little app, I have to define packages for everything rather than just doing like require("./common.js") like you'd expect in a scripting language. https://stackoverflow.com/questions/714063/importing-modules... . The __init__.py thing is weird in of itself because different Py versions have different rules, and without __init__.py you accidentally make it a namespace package: https://stackoverflow.com/questions/448271/what-is-init-py-f...
> Incorrect. `lambda`.
I meant for general functions, not single-line only. Python code tends to have lots of one-off defs. It's common in other langs to pass around anon functions like outer((var){ ... }). Instead Python covered a subset of those use cases with a whole separate context manager feature (`with`). Why are lambdas single-line only, probably because indents-as-scope would make multiline too weird.
> This is like saying "differences between cars and traffic jams". It makes no sense whatsoever.
I'm sure you've done async coding and run into the classic issue of accidentally doing blocking I/O in code that shouldn't be waiting. For a long time, Python had no such thing, so something like a web backend would use a thread pool which adds a lot of overhead the more IO-bound your handlers are. Then they added asyncio to Python. So now the libraries are mixed on whether or not they do things async. Even psycopg2 needed a whole rework to psycopg3 for this, and it still has caveats https://www.psycopg.org/psycopg3/docs/advanced/async.html#as... . Django has some complexity around async vs non-async too. I don't fault Python for not thinking of this back then, but the end result is confusing.
JS had an event loop from day 1, so it's much safer to assume there that IO is non-blocking. This was part of the motivation for Node.js. There's still code predating async-await, but it's easy to wrap that.
That default parameter is a mutable global variable and languages like Rust have effectively banned mutable statics for good reasons that I personally am discovering myself by messing around with UnsafeCell inside statics and even I personally am starting to think that mutable statics are at best a necessary evil, but not one you want to rely on as your go to solution.
For example, why would you expect `Boolean("false")` to equal `false`? It's a string, and bears no relation to the Boolean type. [0]
[1, 2, 3] + [4, 5, 6] == "1, 2, 34, 5, 6"
or parseInt(0.000001) == 0
parseInt(0.0000001) == 1
or "" + 5 == "5"
"" - 5 = -5Well, `+` is a string operator. There's no infix array concatenation operator, so makes sense.
> parseInt(0.000001) == 0 > parseInt(0.0000001) == 1
Never knew about this one! Quite funny actually and I'm curious why that happens. I suppose because `0.0000001` is represented as an exponent rather than a decimal? Although I haven't seen `parseInt` used since 2015, you should use `Number`.
> "" + 5 == "5" > "" - 5 = -5
iirc
`+` operator: - If LHS is a string, concatenate - Otherwise, cast to Number and perform arithmetic.
`-` operator: - Perform arithmetic.
All of these are explainable, and never catch anybody competent out in practice. And, since TypeScript is the norm in a lot of places now, it's never an issue.
The name parseInt suggests it takes a string. Especially "" - 5, why?
They each have different quirks (some will parse 'a123' as 123, others will handle scientific notation etc). The only reliable way of doing this is doing a regex followed by parseInt... which is definitely a footgun IMO.
What makes this weirder than other languages detecting if they’re an import or an invoked file?
__this_file__ == “__launch_file__”
or similar. I understand python values “only one way” of doing things, but it would be helpful for readability.
if __main__:
There are no magic strings to remember and the IDE can help you look up variables.I honestly think one of the main reasons why python is so successful is as simple as:
print "Hello world!"
...and later: print("Hello world!")
It might not seem like much, but when you're asking someone to pick up a language outside of a classroom setting, it's just much more intuitive than: echo "Hello world!";
or: console.log("Hello world!");
or god forbid: public class GFG{
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
These little things matter in the long run, even if they don't seem like they matter when you're already fluent.But Python breaking hello world in version 3 was crazy.
https://nodejs.org/api/esm.html#importmetamain
node also has __dirname and __filename from the good old days.
It makes perfect sense, but also is not an example of what TFA is about. In fact the entire point of having that `if` check is that `__name__` is not a constant.