Jump to content
  • Advertisement


Sign in to follow this  
  • entries
  • comments
  • views

Python is kl but...

Sign in to follow this  


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.
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  


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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!