Sean Pianka Sean Pianka - 9 days ago 5
Python Question

Is the `Builder` design pattern obsolete in Python?

I was reading through this article on design patterns in Scala, and they presented the argument that the Builder pattern is relevant in Java, as it allows for code such as:

CarBuilder carBuilder = new CarBuilder()
carBuilder.setSeats(2)
carBuilder.setSportsCar(true)
carBuilder.setTripComputer(true)
carBuilder.setGPS(false)
Car car = carBuilder.build()


versus the more confusion-prone form:

Car car = new Car(2, true, true, false)


They later stated that:


In a language like Scala which lets you name arguments while passing
them in, the builder pattern is mostly obsolete...


Is this a similar situation for Python, as you're able to name keyword arguments in any call, or is there some reasonable application for this design pattern?

Answer

An authoritative answer can be found in the book Effective Java. Chapter 2 is where the famous Java Builder pattern by Josh Bloch originates. On page 15, it states,

The Builder pattern simulates named optional parameters as found in Ada and Python.

So the answer to your question is an emphatic yes. There is no need to simulate a pattern that exists natively in a language.

You may also be interested in this popular thread: Does Functional Programming Replace GoF Design Patterns?