A little Go => Python story

bearophile bearophileHUGS at lycos.com
Tue Apr 23 16:09:44 PDT 2013


This blog post tells a short simple story of replacing Python 
with Go:

http://blog.repustate.com/migrating-code-from-python-to-golang-what-you-need-to-know/2013/04/23/

Probably the SVM code was already written in C, and just called 
from Python. I don't know if later they have rewritten the SVM 
code in Go, I doubt this.


Its Reddit thread:

http://www.reddit.com/r/programming/comments/1cy3si/what_python_developers_need_to_know_before/

This story shows that it doesn't take a lot for some people to 
switch languages. Go is designed to appeal to Python coders, and 
this helps.
I think Go is simpler than D and this helps Python programmers 
that don't know a lot about the complexities of a system 
languages.

The blog post also lists many problems or troubles in converting 
Python code to Go, some of them apply as well to the converion of 
Python code to D (from this selection I have removed some silly 
things like "Go doesn't allow to have dict values of mixed 
types"):


* No built-in type for sets (have to use maps and test for 
existence)
* In absence of sets, have to write your own intersection, union 
etc. methods
* Python is more forgiving. You can take slices of strings using 
indexes that are out of range and it won't complain. You can take 
negative slices -- not Go.
* No unpacking of a tuple or list into separate variables (e.g. 
x,y,x = [1,2,3])
* Reading Go code is definitely more like a programming language 
whereas Python can be written as almost pseudocode. Go has more 
non-alphanumeric characters and uses || and && instead of "or" 
and "and".
* Writing to a file, there's File.Write([]byte) and 
File.WriteString(string) – a bit of a departure for Python 
developers who are used to the Python zen of having one way to do 
something
* If you're using JSON and your JSON is a mix of types, goooooood 
luck. You'll have to create a custom struct that matches the 
format of your JSON blob, and then Unmarshall the raw json into 
an instance of your custom struct. Much more work than just obj = 
json.loads(json_blob) like we're used to in Python land.


In the Reddit thread they also discuss a bit about a problem of 
Go dicts, but that problem is not present in D:


struct Foo {
     int bar, baz;
}

void main() {
     import std.stdio;
     Foo[string] a;

     a["hello"] = Foo(23, 42);
     writeln("a: hello = ", a["hello"]);
     a["hello"].bar = 0;
     writeln("a: hello = ", a["hello"]);
}


Bye,
bearophile


More information about the Digitalmars-d mailing list