Live data from Hacker News

WTFPython – Understanding Python through surprising snippets

github.com

181–190 of 199 posts

Re: WTFPython – Understanding Python through surprising snippets

#181
post #179
post #177

Earlier quoted context omitted.

The other three lines are not unrelated. If you just take the first three lies (and the last one), it's fine: a = 0 def f(): print(a) f() Maybe this is all obvious to you, but I'll bet you can't name any other language which behaves this way. Compare the original Python to JavaScript (or Lua, Perl, Tcl, Ruby, C, Scheme, Rust, Clojure, or ...): a = 0 function f() { console.log(a) a = 1 } f() f() This one behaves how I…

js also doesn't behave as you want: var a = 9; function foo() { console.log(a); var a = 12; } foo(); $node foo.js undefined The same goes with ruby.

JavaScript is far from perfect, but at least it's clear you declared two variables.

Re: WTFPython – Understanding Python through surprising snippets

#182
post #8

My 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…

This situation is mentioned in the docs: https://docs.python.org/3.8/faq/programming.html#why-do-lamb... "This happens because i is not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined. At the end of the loop, the value of i is 4, so all the functions now return 4 2, i.e. 16." These sort of scoping gotchas are pretty common across all progr…

Any feature that is a "great argument for unit testing" is a poor feature.

Re: WTFPython – Understanding Python through surprising snippets

#183
post #179
post #177

Earlier quoted context omitted.

The other three lines are not unrelated. If you just take the first three lies (and the last one), it's fine: a = 0 def f(): print(a) f() Maybe this is all obvious to you, but I'll bet you can't name any other language which behaves this way. Compare the original Python to JavaScript (or Lua, Perl, Tcl, Ruby, C, Scheme, Rust, Clojure, or ...): a = 0 function f() { console.log(a) a = 1 } f() f() This one behaves how I…

js also doesn't behave as you want: var a = 9; function foo() { console.log(a); var a = 12; } foo(); $node foo.js undefined The same goes with ruby.

You’re sidestepping the problem. The problem is that Python uses the same syntax for variable declaration and variable assignment, and this can lead to unexpected behavior. That’s far from normal in the programming world, making it more unexpected.

Re: WTFPython – Understanding Python through surprising snippets

#184
post #8

My 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…

In a decade of python use I can probably count on one hand the number of times lambda has been a good solution to a problem I had. Many times I end up re-writing them as regular functions for clarity sake.

What's the symptom and what's the cause? Python's lambdas aren't very good, so they're frequently not a very good solution...

Re: WTFPython – Understanding Python through surprising snippets

#185
post #177

Earlier quoted context omitted.

Well it's f that has the problem, rather obviously, and the other three lines are unrelated. So I don't see how this is some especially simple way of triggering that error.

The other three lines are not unrelated. If you just take the first three lies (and the last one), it's fine: a = 0 def f(): print(a) f() Maybe this is all obvious to you, but I'll bet you can't name any other language which behaves this way. Compare the original Python to JavaScript (or Lua, Perl, Tcl, Ruby, C, Scheme, Rust, Clojure, or ...): a = 0 function f() { console.log(a) a = 1 } f() f() This one behaves how I…

Of course I'm not a good judge, I have been programming Python since 1.4 and my colleagues also all have Python experience.

But at least I like the idea that it gives an error message when confronted with ambiguity. That way it doesn't do something you didn't expect silently. Sadly it only gives it at runtime, not at compile time.

Re: WTFPython – Understanding Python through surprising snippets

#186
post #8

My 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…

"Excess syntactic sugar causes cancer of the semicolon."

GJS: Gerry Sussman attributes this quote to Alan Perlis.

_Structure and Interpretation of Computer Programs_: https://mitpress.mit.edu/sites/default/files/sicp/full-text/...

Re: WTFPython – Understanding Python through surprising snippets

#187
post #8

My 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.

Lua and Perl use fresh variables for each iteration, instead of mutating a shared copy.

Re: WTFPython – Understanding Python through surprising snippets

#188
post #117
post #82

Earlier quoted context omitted.

The new walrus operator now makes indentation obsolete. You can write averything within one line as an array and it is hard to read. This in combination with syntactic sugar, operator overloading and unicode variables can make the language very hard to read. e.g. # compute pi 1000000 >> ψ( ψ(χ>>op("(x**2+y**2)**0.5 >Σ*4>> _/_) or this: # 10 fibonacci numbers [x:=[1,1]] + [x := [x[1], sum(x)] for i in range(10)] is va…

in principle you could always write one-liners like this with lambdas or list comprehensions by translating let x = in into this: (lambda x: )( ) (as the lambda calculus does) or this: [ for x in ( ,)][0] of course the readability is terrible, but the power was always there if you wanted to (ab)use it :) (yeah, i've used the second one in a repl, don't @ me)

Indeed, a single python expression has been enough for Turing completeness for a long time now. I wrote a brainfuck interpreter in one once.

Re: WTFPython – Understanding Python through surprising snippets

#189
post #4

Maybe "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.

I try to keep my data science work in Python limited to short scripts for reading a csv and then making some matplotlib figures. I personally find it unsuitable for anything beyond that

Re: WTFPython – Understanding Python through surprising snippets

#190
post #65

Earlier quoted context omitted.

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…

Python does have a lot of problems, but it's so far the most flexible language like English. High level programming language is meant to communicate, not to dictate the computation. Python happens to be easy to understand by humans.

I don't disagree with anything you've said, but in my opinion strongly typed function definitions/returns are about communication rather than dictation - I'd rather the compiler tell me that I'm passing an unexpected argument type rather than the runtime executor.

But to contradict your point, there's also a fair amount of Python which is a dictation - Guido even called his job role 'Benevolent dictator for life'. I really hated some of the guidelines as a new programmer but after dealing with them for some time I understand that consistency is far more important than preference in many/most/all cases.

Post reply on HN