From: Gavin Sinclair Date: 2004-04-06T00:08:12+09:00 Subject: Re: deciding between ruby and python On Monday, April 5, 2004, 5:04:18 PM, Ville wrote: > (Yes, I know I'm really overstaying my welcome in this ng, just > "trolling" a 0.02E more) I don't mind. You post intelligently, and although a lot of this stuff is pretty subjective, I think there are yet more objective observations to be teased out. > Sinclair> for s in uniq(sort(map(grep(........))))) :) > And truly, I don't see anything wrong with that code. It's not > 'perlish', it's the plain old functional way of doing things. It's > chaining just like with method calls. So it's the functional way of doing things. I've got nothing against that, but I'm not interested in it. I (much) prefer Ruby's OO style. > >> Ruby iterator/blocks and python iterators/generator are very > >> different approaches that yield similar results somehow, but > >> both are really valuable and I don't think one is inherently > >> better than the other. > Sinclair> I argue that Ruby's is better because it's more general > Sinclair> (provides features beyond mere iteration), more OO (thus > Sinclair> a better fit with the language), and more high-level > Sinclair> (see below). The different approaches > Blocks provide iteration and anonymous functions. Both exist in > Python. And if the needs of your anonymous function exceed what lambda > provides, you can use what I call big L notation: > def L(matchobj): > return matchobj.group(0).upper() > res = re.sub('u|a', L, 'huijaahuijaa') That's a poor man's substitute. Seriously. It means you have to name everything you're doing, and pollute the function namespace *for no good reason*. Yes, you can "do it", but it's ugly in comparison. And there is an insurmountable limitation (I think): # Count the characters, words, and lines in the given string. def count(str) chars = words = lines = 0 str.each do |line| chars += line.chomp.length words += line.split lines += 1 end return [chars, words, lines] end Can you provide an implementation in the same style in Python? It's not that I don't believe Python can count characters, lines and words in a string. But I don't believe it can carry outside state into its "substitute for blocks". What about this example, demonstrating various database capabilities? require "dbi" # Connection: the database connection is automatically released at # the end of the block, including if there is an exception therein. DBI.connect(conn_str, user, passwd) do |dbi| # Prepared statement: the resource is automatically freed at the # end of the block. total_distance = 0 dbi.prepare("SELECT road_distance from MAP_INFO where CITY1 = ? and CITY2 = ?") do |sth| sth.select("Sydney", "Melbourne") do |row| total_distance += row['distance'].to_i end sth.select("Melbourne", "Adelaide") do |row| total_distance += row['distance'].to_i end end puts "Road distance from Sydney to Adelaide via Melbourne is #{total_distance} km." # Transaction support: any exception in this block will cause a # "ROLLBACK". If all is fine, a "COMMIT" is performed at the end. dbi.transaction do |dbi| dbi.do("UPDATE bank_account SET balance = balance - 50 where name = 'alice'") dbi.do("UPDATE bank_account SET balance = balance + 50 where name = 'bob'") end end I'd like to see the same program written in Python. I don't know the Python database interface, but I expect it would need to have explicit exception handling. There would also be no automatic releasing of resources. I could be very wrong. But assuming I am correct, then the above program demonstrates Ruby operating at a higher level of abstraction than Python is capable of. All the yucky stuff like resource management and transactions are taken care of by the library, not in the top-level code. That is a perfect demonstration of "higher level programming". That is my frame of reference when it comes to high-level programming. > Sinclair> Don't get me wrong, Python is a high-level language. > Sinclair> But Ruby is a very high-level language. > Oh, please. That wouldn't be true even if Ruby afficiandos kept > repeating it 10 times a day (it could even become the new "Python OO > is a hack"). Ruby and Python are pretty much exactly on the same level > abstraction-wise. Ruby is higher level than Python in the sense Perl > is higher level than Python, i.e. it provides some "conveniences" of > questionable utility like regexps in language syntax and multiple > methods doing the same thing. It is true, and a Python fan denying it 10 times a day doesn't make it false. To prove me wrong, please present your best effort at translating the database programming example above into Python. Of course, you may disagree with my frame of reference instead, but I think that's pretty good. Cheers, Gavin P.S. Let me restate that my knowledge of Python is very small compared to yours, and I am happy to learn more about it. And at the end of the day, if (when) we agree to disagree, I won't be claiming that this discussion has any real significance. It's just interesting while it lasts. I'm not a zealot; just keen on understanding the finer points of life and/or computer programming :)