Archive for June, 2008

Wow the things we find humorous…

0

Oh Python… how I loathe thee…

2

So those who know me know that I am an avid ruby fan, you may not know it by looking at some of my code but I am, trust me. I have spent the last 2 or so years getting to the point where I am not embarrassed to share my work with others. Now… the job comes in and says “Python is the new hotness”, granted not new, hell not even old hotness but they think so all the same. Now all new projects are in python and python only. “Ok” I says, “I can do python if I have to”.

As I sit here recoding a bunch of libraries I had finely tuned in ruby to python I realize how much I really hate python. I think it is a travesty that the developers of this language are able to call it Pure Object Oriented when core types aren’t really objects. Lets take for instance strings, a string is an object in ruby, hell everything is an object in ruby buy in python a string doesn’t even inherit the ability to understand what it is. As an object you are a member of a class of some kind. Being a member of that class gives you certain abilities to work on yourself that are inherited by being a member of that class. We won’t dig too deeply into this but look at the following:

Ruby:

irb> "Hello World!".class
<br>
=> String

Python:

>>> "Hello World!".class()
<br>
SyntaxError: invalid syntax
<br>
>>> type("Hello World!")
<br>
<type 'str'>

So looking at the previous snip you see that in Ruby a string is an object of the String class and inherits the ability to know that it is a member of the class String. In Python we have to call a core method to look at the data to figure out what type it is. This may be reaching but why in the hell can’t a string know it is a string?

Let’s take this further, you would think that in any pure object oriented language an object can figure out how many characters or items it contains no?

Ruby:

irb> "Hello World!".length
<br>
=> 12

Python:

>>> "Hello World!".length()
<br>
AttributeError: 'str' object has no attribute 'length'
<br>
>>> len("Hello World!")
<br>
12

Now we see even a basic function such as knowing the data length of one’s self isn’t permitted in python, we don’t completely inherit, we selectively inherit. And again we are punished by being forced to call an outside method to look at our data.

Big whoop you may say, but nay my friends there is a method to this madness. Lets say you were tasked with taking a string, figuring out it’s length, taking it’s length and coverting that to a string. Why would you do this? Who the hell knows but look at the difference in the code to do so:

Ruby:

irb> "Hello World!".length.to_s
<br>
=> "12"

And to prove it is a string that you are looking at, besides the handy quotes around it:

irb> "Hello World!".length.to_s.class
<br>
=> String

Python:

>>> str(len("Hello World!"))
<br>
'12'

And to prove again it is a string that you are looking at…

>>> type(str(len("Hello World!")))
<br>
<type 'str'>

So you tell me, what makes more sense in the grand scheme of things?

“Hello World!”.length.to_s.class

or

type(str(len(“Hello World!”)))

I submit to you that the language wars are over! Python has killed itself alongside Perl. Both great ideas that slapped features on their sides like colostomy bags without ever thinking about building real core functionality.

Go to Top