|
Python's dynamic typing saves you a lot over static typing without parametric polymorphism, as in Go's case. As a concrete example, take sorting. In Go, you have to implement `sort.Interface` for each custom type you define. An example taken from the `sort` package: package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func (p Person) String() string {
return fmt.Sprintf("%s: %d", p.Name, p.Age)
}
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func main() {
people := []Person{
{"Bob", 31},
{"John", 42},
{"Michael", 17},
{"Jenny", 26},
}
fmt.Println(people)
sort.Sort(ByAge(people))
fmt.Println(people)
}
An equivalent in Python is quite a bit simpler: class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return "%s: %s" % (self.name, self.age)
people = [
Person("Bob", 31),
Person("John", 42),
Person("Michael", 17),
Person("Jenny", 26),
]
print people
people.sort(key=lambda person: person.name)
print people
If it were just sorting, it wouldn't be that big of a deal. But there's a lot of nice abstractions missing from Go due to the lack of generics - e.g. higher-order functions like `map`. |
But we are doing something you can't do at all in Python (barring specialized array modules or similar).
The equivalent of the Python code would be something like:
where we have a library function This is exactly the same, expect for instead of saying "Give me .age" like we do in Python, we have to explicitly cast and say "I'm expecting a person, give me .Age".