The Python __str__ method and the Go String method: A comparison

DSL
3 min readJan 6, 2023
image credited by https://hakaimagazine.com

perquisites to reading this article

1: basic understanding of python overloading and the __str__ method in class definition. review the concept

2: basic understanding of pointers in Go. review the concept

In Python and Go, we often need to customize the string representation of a value. In Python, you can do this by defining the __str__ method in a class, while in Go you can implement the String method of the fmt.Stringer interface.

In this tutorial, we will compare the way you can customize the string representation of a value in Python and Go. We will look at examples of how to define the __str__ method in a Python class, and how to implement the String method in a Go type. We will also see how you can use the str function and the fmt package to print the string representation of a value in both languages.

By the end of this tutorial, you will have a good understanding of how to customize the string representation of a value in Python and Go, and how to use the __str__ method and the String method to achieve this.

customize the string representation

In Python, you can customize the string representation of an object by defining the __str__ method in the object's class. The __str__ method should return a string, and it will be called when the str function is applied to an instance of the class, or when the instance is used in a string context, such as when it is printed.

Here is an example of how to define the __str__ method in a Python class:

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __str__(self):
return f'Point({self.x}, {self.y})'

print(point) # calls point.__str__()
print(type(point)) # <class '__main__.Point'>
print(str(point)) # Point(1, 2)
print(type(str(point))) # <class 'str'>

In Go, you can achieve a similar result by implementing the String method of the fmt.Stringer interface. The String method should return a string, and it will be called when the value is passed to a function like fmt.Println or fmt.Sprint.

Here is the equivalent example in Go:

type Point struct {
x int
y int
}

func (p *Point) String() string {
return fmt.Sprintf("Point(%d, %d)", p.x, p.y)
}

point := &Point{1, 2}
fmt.Println(point) // calls point.String()

In this case, it is worth pointing out that if the String() function is not defined in the code above, the fmt.Println(point) will generate the output as &{1 2} instead of Point(1, 2) .

custom string representation for a built-in type

You can also define a custom string representation for a built-in type, like an integer or a slice, by creating a type that wraps the built-in type and implementing the String method for the new type.

Here is an example in Python:

class WrappedInt:
def __init__(self, value):
self.value = value

def __str__(self):
return f'WrappedInt({self.value})'

wrapped = WrappedInt(42)
print(wrapped) # calls wrapped.__str__()
print(str(wrapped)) # calls wrapped.__str__()

And here is the equivalent example in Go:

type WrappedInt int

func (i WrappedInt) String() string {
return fmt.Sprintf("WrappedInt(%d)", i)
}

wrapped := WrappedInt(42)
fmt.Println(wrapped) # calls wrapped.String()

Conclusion

In this tutorial, we have looked at the way you can customize the string representation of a value in Python and Go. We saw how to define the __str__ method in a Python class to provide a custom string representation of an object, and how to implement the String method of the fmt.Stringer interface in Go to achieve a similar result. We also learned how to use the str function and the fmt package to print the string representation of a value in both languages.

Customizing the string representation of a value can be useful when working with complex data structures or custom types, as it allows you to control how the value is represented as a string. By using the techniques shown in this tutorial, you can easily customize the string representation of a value in Python and Go to suit your needs.

--

--

DSL
DSL

Written by DSL

Sr software engineer. Love in Go, JavaScript, Python, and serverless AWS. Follow me for tech insights and experiences. follow me on twitter @terraformia

No responses yet