Sign in to follow this  
  • entries
    122
  • comments
    246
  • views
    89777

Python is kl but...

Sign in to follow this  

185 views

I just seen Washu's post on how he loves Python but do not find his argument very convincing. I would reply but the post is old so I decided to do it here. His main argument in *that* post is that Python has very little noise in its code. In my opinion that too ranks amongst the highest in criteria for a good language but is not a very distinguishing feature. In his post Washu anticipates people like myself who would "point to the many functional languages that have many of the same capabilities". Which is just what I am going to do.

The python code is
items = [
{'name' : "Bronze Sword", 'value' : 50, 'diceCount' : 1, 'diceSides' : 4},
{'name' : "Steel Sword", 'value' : 100, 'diceCount' : 2, 'diceSides' : 4},
{'name' : "Adamantium Sword", 'value' : 200, 'diceCount' : 1, 'diceSides' : 10}
]

result = [item for item in items if item['value'] > 50]
print result






In F# the code is just as readable. As a personal opinion I find it more readable because the quotes around property names in the python code threw me a tiny bit and the list comprehension syntax is clearer IMHO.
#light
type Item = {name : string; value : int; diceCount: int; diceSides : int}

let items = [
{name = "Bronze Sword"; value = 50; diceCount = 1; diceSides = 4};
{name ="Steel Sword"; value = 100; diceCount = 2; diceSides = 4};
{name = "Adamantium Sword"; value = 200; diceCount = 1 ;diceSides = 10}
]

let result = [for item in items when item.value > 50 -> item ]*
print_any result






F# also has a very lightweight whitespace based syntax and hassless integration with the .NET codebase. All without having to give up the benefits of static typing. The definition of the Item type also helps others (including the compiler) who might touch the code to know the relevant parts of Item in their entirety.

A final word is that the addition of the For Each extension method in the C# example is not fair since this is utility functionality that does not belong in the example. While it is useful it probably would be found in some other file while for a quick test, especially since it does not really reduce the amount of noise in the code (actually adds more) I do not think it belongs.

I guess my point is that python's clean syntax is more a yeh so, I can too feature. Not something particularly striking.

*The code is deprecated and his been replace with the slightly less (IMO) clear [for x in xs do if then yeild ]
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now