r/programming • • Aug 30 '08

Tour De Babel -- Rant about programming languages (at Amazon)

http://steve.yegge.googlepages.com/tour-de-babel
13 Upvotes

122 comments sorted by

View all comments

Show parent comment

12

u/martincmartin Aug 31 '08

As someone who has used both (and also contributed to Groovy), I think the biggest difference is that Ruby has real anonymous functions, that can have an arbitrary number of statements/expressions in them. It makes dealing with any sort of collection much easier. Python's list comprehensions seem timid in comparison: they can only do "collect" and "grep," with single expressions, and the syntax for combining them is different than the rest of the language.

In Ruby, you can write a function which takes a file name and an anonymous function as arguments, then opens the file, passes it to the anon function, then closes the file in a "finally" block. It's much nicer than having to have explicit finally blocks.

0

u/[deleted] Aug 31 '08

[deleted] — view deleted comment

2

u/Niten Aug 31 '08 edited Aug 31 '08

The difference is mostly syntactic sugar. Python doesn't have multi-line lambdas, so you have to name your function before you pass it in to file_funct:

def funct(myfile):
    do_stuff_to(myfile)
    do_other_stuff_to(myfile)

file_funct(filename, funct)

In Ruby, on the other hand, you can send file_method an anonymous block, rather than define and then pass it a named method:

file_method(filename) { |myfile|
    do_stuff_to(myfile)
    do_other_stuff_to(myfile)
}

This makes things a bit cleaner, especially since Ruby has such nice syntax for passing anonymous blocks to methods. But it doesn't really change what you are able to accomplish in your code.

1

u/Smallpaul Sep 01 '08

In Python that would just be:

with file_method(filename) as myfile:
   do_stuff_to(myfile)
   do_other_stuff_to(myfile)

1

u/almkglor Sep 01 '08 edited Sep 01 '08

Out of curiousity, how generic is "with" in Python?

Suppose I have a unwinding process for a function (scheme):

(let ((foo #f))
  (dynamic-wind
    (lambda () (set! foo (constructor)))
    (lambda () (do-stuff-to foo)
               (do-other-stuff-to foo))
    (lambda () (destructor foo))))

Is there a generalized syntax which allows for such a constructor/deconstructor style? (in the case of languages which support full continuations, this also performs cleanup upon exiting via an external continuation; I believe the Ruby finally block also handles this properly)

1

u/Smallpaul Sep 02 '08

Python does not allow a function to take three anonymous blocks except as actual functions. The same is true for Ruby. It has been a long time since I used scheme so I may have misinterpreted the question.

1

u/almkglor Sep 03 '08 edited Sep 03 '08

Err, what I mean is, dynamic-wind ensures that the given three functions are executed in order. In particular, if the middle function exits abnormally (via an error or a continuation call), the last function is still invoked (i.e. cleanup), similar to Ruby finally clauses.

A feature of dynamic-wind is, if a continuation is taken within the middle function, and that continuation is called from outside the dynamic-wind call, then the first function gets called on entry to the continuation. It's this bit which I don't think is present in other languages.

It doesn't have to be functions per se, just a generic way of saying "do this bit before you ever ever enter that bit, and make sure to do this other bit no matter what happened to that bit".

6

u/jmhodges Aug 31 '08

mcantelon, that ^ pretty much. It sounds weird, because it is weird when you first run into it. It's a bit different than passing functions around (even anonymous ones in functional languages) because the syntax to do this.. feels different.. cleaner. It's hard to explain, but when you see it and grok it, its a very nice way of seeing a problem in a simpler, easier to understand way.

Oh, and everything is an object and really, really an object and holy christ is it awesome not to have to pass self around everywhere. And modules rock.

I should note that I still like python. Ruby just has some things that are nifty that python doesn't.

5

u/ubernostrum Aug 31 '08 edited Aug 31 '08

Oh, and everything is an object and really, really an object

Just like Python, you mean?

Edit: and... what? I get downmodded for pointing out that the same is true in Python? Show me something in Python that isn't an object, then.

3

u/shen Aug 31 '08

I know that Python uses objects for everything, but the difference here is that Ruby consistently uses object-oriented syntax, while Python doesn't (list comprehensions, blocks instead of loops, a few sticking-out functions like len, for example).

2

u/ubernostrum Aug 31 '08 edited Aug 31 '08

And that's not the same thing at all. There's a big difference between saying, say, that Ruby uses message-passing syntax everywhere and Python doesn't use method-calling syntax everywhere (almost true about Ruby, true about Python -- there are situations where Ruby doesn't use consistent syntax) and saying, say, that in Ruby everything is an object and implying that in Python not everything is an object (which is false).

3

u/shen Aug 31 '08

I know that. I'm just saying that people generally mean the former when they say the latter.

1

u/ubernostrum Aug 31 '08

People should say what they mean instead of casting about dangerously-incorrect statements.

3

u/[deleted] Aug 31 '08

print

1

u/Leonidas_from_XIV Aug 31 '08

Won't be in Python 3.0, your complaint is ages old and has been heard by the developers.

3

u/[deleted] Aug 31 '08

I was answering a question, not complaining.

1

u/Smallpaul Sep 01 '08

Keywords are not objects in Python. Nor are they are in Ruby. Ruby has more keywords than Python.

3

u/[deleted] Aug 31 '08 edited Aug 31 '08

+ ?

5

u/ubernostrum Aug 31 '08 edited Aug 31 '08

That operator delegates to the __add()__ method of its left-hand operand; a + b is literally the same as a.__add__(b), just with nicer syntax. And unless I'm mistaken, Ruby has no standalone, built-in object named "+" (instead using it as syntax to delegate to a method call, just like Python), so if this is an argument against Python it's one against Ruby as well.

1

u/[deleted] Aug 31 '08 edited Aug 31 '08

I know it delegates to the __add()__ method of its left-hand operand. It's not an object though (or is it? to be honest, I don't know python internals well enough to know. It seems like it's just a symbol-table entry to me)

In the case that I am mistaken, what about def, or a comment? There's tons of stuff in python that aren't objects. If keywords and comments are objects in python, why can't I treat them like objects? Or can I?

3

u/ubernostrum Aug 31 '08

OK, then, as long as you're willing to make exactly the same critique of Ruby (i.e., that some syntactic elements are not in fact objects), I'm OK with that.

Typically what I think when I hear "everything is an object", though, is that everything a program will be accessing or manipulating at runtime is an object, not that every byte in the source code file ultimately corresponds to some object in the program at runtime, and I think that's much more in line with what people mean when they use that phrase (because, honestly, by the line of reasoning you're taking you wouldn't even be able to say "everything" in Smalltalk is an object, which would be absurd).

2

u/[deleted] Aug 31 '08

I would make the same critique of Ruby if someone asked me to show them something in Ruby that wasn't an object. (I code mainly in Python at this point in time)

I agree with the second paragraph of your comment, although part of me doesn't think that people should be saying "everything is an object", unless "object" is the one base thing that everything is derived from in the language specs.

Squeak, which is the only smalltalk implementation I'm even remotely familiar with, seems to come awfully close to having everything that the programmer interacts with being an object (unless, presumably, they're working on squeak outside of squeak) - but I could be sorely mistaken about that.

Lisp (specifically CL) would be another similar example - not of everything being an object, but of everything being definable through a very few simple abstractions.

→ More replies (1)
→ More replies (1)

2

u/Tuna-Fish2 Aug 31 '08 edited Aug 31 '08

python 3:

with open("file.txt") as file: doSomething(file)

or locking:

with myLock: doSomething()

http://docs.python.org/dev/3.0/reference/compound_stmts.html#the-with-statement