Earlier quoted context omitted.
Interesting, I don’t use python day-to-day, so I had assumed that the delayed capturing of the iteration variable was a lambda thing. Looks like this is still broken: for i in range(10): def mul(x): return i^x powers_of_x.append(mul) That to me coming from a JS background is totally wild
Why would it be "totally wild" coming from a JS background when the corresponding JS code: for(var i=0; i i^x); } behaves the exact same way for the same reason? By definition, a closures closes over its definition context, it doesn't capture the values at definition time but instead keeps referring to said definition context. If the definition context is mutable and modified, the closure reflects that change when it…
WTFPython – Understanding Python through surprising snippets
61–70 of 199 posts
Re: WTFPython – Understanding Python through surprising snippets
#62Earlier quoted context omitted.
Unfortunately, this is not true. Although the equality operators are a favorite bikeshedding topic, the largest source of errors in JS code bases are type errors. Using a "non-sane" equality check, such as those with type coercion would actually mask or alleviate these source of bugs you mention.
I was thinking more of the case where the types match but objects and arrays with the same contents are considered different. I’ve watched every member of my team get stung by it again and again - and then have to create workarounds to get past it.
Re: WTFPython – Understanding Python through surprising snippets
#63Earlier quoted context omitted.
A while ago, I was also sceptical about the walrus operator, although I use assignments in expressions all the time in C++. But when I was perusing a library written Python the other day, I found five spots in which the walrus operator would make sense - eliminating one line of source code without readability suffering from it.
Is saving 5 lines of code really worth breaking Python's "one way to do it" design principle?
> There should be one — and preferably only one — obvious way to do it.
Which is a very, very different assertion. And there remains one — and exactly only one — obvious way to do an assignment, because the walrus operator is literally invalid syntax as a statement:
>>> [a:=1]
[1]
>>> a:=1
File "", line 1
a:=1
^
SyntaxError: invalid syntaxRe: WTFPython – Understanding Python through surprising snippets
#64Re: WTFPython – Understanding Python through surprising snippets
#65If only it had curly braces instead of just indention. God, that kills me. And used unicode instead of ascii as the default. And there wasn't python 2.7 vs 3. Someone help me stop this list.
I've coded in python for >10 years and C# for ~half of that. I prefer indentation-based control flow purely because it condenses code vertically and creates consistent indentation with actual purpose (many of the devs on C# projects I've dealt with have had inconsistent tab/space settings and no linting). If anything, the one thing I hate about Python3 is dynamic typing - inferring datatypes in function bodies is gre…
High level programming language is meant to communicate, not to dictate the computation. Python happens to be easy to understand by humans.
Re: WTFPython – Understanding Python through surprising snippets
#66Maybe "beautifully designed" is a bit much then In my experience Python is not very nice to work with. I do like the ecosystem for data science though, it's just amazing how much there is. Hopefully the language will grow into something better now that the dictator stepped down.
Second this. I can’t understand finding Python beautiful or even elegant. It just took a hodgepodge of features from languages like Haskell and C++, and repackaged them clumsily. It’s good for writing short scripts and throw-away code, but it shouldn’t be used as a serious programming language.
Re: WTFPython – Understanding Python through surprising snippets
#67Earlier quoted context omitted.
A while ago, I was also sceptical about the walrus operator, although I use assignments in expressions all the time in C++. But when I was perusing a library written Python the other day, I found five spots in which the walrus operator would make sense - eliminating one line of source code without readability suffering from it.
Is saving 5 lines of code really worth breaking Python's "one way to do it" design principle?
Re: WTFPython – Understanding Python through surprising snippets
#68Earlier quoted context omitted.
`a += b` does something along the lines of `a = a.__iadd__(b)`. Here `__iadd__` performs the actual append, but then the assignment fails.
Does it though? So why doesn't a += b change the object id id(a) ?
list.__iadd__ is (sadly) defined as something like
def __iadd__(self, other):
self.extend(other)
return self
So it's possible to have __iadd__ itself succeed modifying the list in place but then the "no-op" reassignment fail.[0] like many data model operations there's really a bunch of fallbacks depending on what is and is not implemented
Re: WTFPython – Understanding Python through surprising snippets
#69My biggest complaint about Python is that it somehow doesn’t get flak for having the same (if not worse) scoping as JS, which gets endless hate for its function-scoped variables. (So much so that block scoped variables are the new normal in JS, but not in Py!) Take for instance: >>> powers_of_x = [lambda x: x^i for i in range(10)] >>> [f(2) for f in powers_of_x] [512, 512, 512, 512, 512, 512, 512, 512, 512, 512] To m…
I don't find this at all unexpected. In an imperative language I expect a loop to be implemented by mutating a common variable.
Re: WTFPython – Understanding Python through surprising snippets
#70Earlier quoted context omitted.
UTF-8 is the current default.
It uses unicode but it doesn’t use UTF-8 by default as external encoding and never as internal encoding except for an optional secondary buffer.