Discussion on: 3 Tricky Python Nuances

veky profile image

OMG. Every "nuance" you've written about has something (detail or the main topic) you got completely wrong. Just briefly:

  1. "references" are simply names for objects, and have nothing to do with pointers (though, of course, if you're writing Python in C, you would probably implement them using pointers, but there's absolutely nothing in the language specification that says it must be so). The reason why [[0]*5]*5 constructs only two lists is simple: you only have two pairs of brackets there (and you evaluate each only once). If you need 6 lists, you need to execute brackets (or list call) six times. You can do it sequentially or in a loop/comprehension, but surely not via an arithmetic operator. (Imagine if 6*7 really constructed six different sevens and then added them.:)

  2. "when the function is first declared" is wrong. Default is evaluated every time def statement is executed. You can put your def in a loop, and you'll see the default evaluated every time. Python does have declarations (global, nonlocal and annotations), but def is not one of them. It is simply an executable statement.

  3. id values can be reused for objects with nonoverlapping lifetimes, so checking id(a) == id(b) doesn't have to be the same as a is b. More importantly, checking if a is True is quite different than checking if a. It's as if you said "my engineering managers had a real stick in their craw about always using * operators with integers to the point of rejecting PRs that didn't follow this pattern". Sometimes you use *, sometimes you use +. They simply do different things.