Skip to content

Instantly share code, notes, and snippets.

View fralau's full-sized avatar

Laurent Franceschetti fralau

  • SettleNext
  • Geneva, Switzerland
View GitHub Profile
@fralau
fralau / GoldenString.md
Last active March 29, 2018 13:25
Golden String (pattern):

Golden String: A Python design pattern to salvage a function returning a string

Problem:

What to do if you have a well-used API with a function returning a string, and you want to return other information without breaking the calling code?

Solution

Principle

Pass that information as attributes to the string itself! Since the standard str type does not allow it, a new class GoldenStr must be created.

In the called function

In the called function, you could use the code below to write: