4 ms·
I posted this there, but I feel more people prefer to read comments here. --- First, I love both Python and Clojure, and completely agree that Clojure is more
by bretthoerner 17y ago
I posted this there, but I feel more people prefer to read comments here.
---
First, I love both Python and Clojure, and completely agree that Clojure is more functional and aimed towards concurrency (this was its goal, after all). But I feel like you're doing Python wrong here.
> I think this should make a Pythonist a little jealous
No, I think you misunderstood the whitespace rules for Python. First, it isn't set to "4 spaces", it just has to be uniform. You could use 8 tabs if you wanted, or 2 spaces, or 7 spaces. Also, inside of data structures, strings, function calls, etc you can go whitespace crazy. This is valid Python:
users = (# name , id, age, phone
("Frank", 200, 22, "555-1234"),
("Johnathan", 201, 24, "555-3213"),
("Peter ", 202, 101, "555-2312"))
> The last print statement outputs "10" which is surprising to me, since y has never been declared in that given scope.
But it wasn't declared in that scope, there is no scope for 'if'. It may not be what you're used to but you can't blame Python for your assumption of what should have it's own scope or not.
> To resolve it you have to manually call 'del y'
What do you mean to resolve it? Why are you deleting the reference at all? You very rarely see 'del' in real Python code.
> but I can't count the number of C app's that have suffered from memory-leaks and what not, based on the lack of automated garbage collection
What does that have to do with Python? It is reference counted, those values will be deleted when they go out of all scopes, just like in Clojure.
> But this is very different from Clojures concept where immutable means: 'x' never changes.
That isn't what it means in Clojure at all.
user=> (def x 1)
#'user/x
user=> x
1
user=> (def x 5)
#'user/x
user=> x
5
I just changed 'x'. What immutability means is that the value 'x' points to does not change. And that's exactly what it means in Python, too, but dictionaries are mutable (and this is documented). For example, strings in Python are immutable, just as in Clojure. If two things reference the same string and one "changes" it only means that it references a wholly new string.
> Guido is not preparing to transition into modern programming
That's a little extreme. Concurrent programming is valuable, but there will also be many programs that fit single process / thread models very well until the end of time.
- dreish 17y agoI agree for the most part, but incidentally you would never (def x 1) and then (def x 5) in a running program -- you would use an atom, ref, or var instead. Redefinition of a symbol is permitted so you don't have to quit and restart your REPL every time you rewrite a function. A more meaningful objection is that you can shadow a local with a different local in Clojure, like this: (let [a (some-calculation) b (something-else) a (some-function-of a b)] a) which is essentially what is being done in the Python code. I don't think there are many strong arguments to make in favor of Clojure over Python other than concurrency and macros, and maybe the immutable hashes.
- bretthoerner 17y ago> you would never (def x 1) and then (def x 5) in a running program Right, I understand. It was the way the author stated 'x never changes' that made me believe he doesn't understand the difference between binding a name and changing a value.
- mr_dbr 17y agoThe whole sentence regarding an if blocks (lack of) scope seems.. wrong. > .. since y has never been declared in that given scope. I don't quite know how Pythonists handle this, but I can imagine many situations where this will be the cause of much confusion Given the following code: if some_irrelevant_function(): x = 2 else: x = 5 print x The author is claiming it would be less surprising for that to raise a NameError exception, complaining x is undefined? > To resolve it you have to manually call 'del y' No. You just have to be sane with the usage of your variables. I have never run into a situation where this has been a problem. > I don't have the numbers for Python, but I can't count the number of C app's that have suffered from memory-leaks and what not, based on the lack of automated garbage collection. With Pythons use of scope, I imagine quite a few bugs will follow. Err, no. ---- > Now in one Python doc I found they actually refer to this type of data [dictionary] as immutable Assuming the docs actually say this, they are incorrect and should be amended.. Dictionaries are absolutely mutable. > Lambda Err. The complaint here is you cannot use "?" in an identifier...? > To Pythons credit it actually pinpointed the exact character which caused the exception - You shouldn't except that much help from Clojures backtraces....yet Not to make this into a "languageX vs languageY" debate, but I'd say having decent error messages is far preferable to being able to use symbols in an identifiers name..
- tetha 17y agoRegarding concurrency: check out multiprocessing. Very, very simple access to process concurrency to do things.