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.
WTFPython – Understanding Python through surprising snippets
181–190 of 199 posts
Re: WTFPython – Understanding Python through surprising snippets
#182My 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…
Re: WTFPython – Understanding Python through surprising snippets
#183Earlier 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.
Re: WTFPython – Understanding Python through surprising snippets
#184My 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.
Re: WTFPython – Understanding Python through surprising snippets
#185Earlier 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…
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
#186My 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…
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
#187My 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
#188Earlier 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)
Re: WTFPython – Understanding Python through surprising snippets
#189Maybe "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.
Re: WTFPython – Understanding Python through surprising snippets
#190Earlier 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.
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.