Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is factory pattern meaningless in Python?

Since Python is a duck-typed language is writing factory classes meaningless in Python? http://en.wikipedia.org/wiki/Factory_method_pattern

like image 768
Cenk Alti Avatar asked Jul 18 '11 19:07

Cenk Alti


People also ask

Should you use factory pattern?

Advantage of Factory Design Pattern Factory Method Pattern allows the sub-classes to choose the type of objects to create. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

Does Python use factories?

Usage of the pattern in Python Usage examples: The Factory Method pattern is widely used in Python code. It's very useful when you need to provide a high level of flexibility for your code. Identification: Factory methods can be recognized by creation methods that construct objects from concrete classes.

What are the consequences of applying the GOF factory method pattern?

Here are two additional consequences of the Factory Method pattern: Provides hooks for subclasses. Creating objects inside a class with a factory method is always more flexible than creating an object directly. Factory Method gives subclasses a hook for providing an extended version of an object.

How do you create a factory pattern in Python?

The factory pattern comes under the creational patterns list category. It provides one of the best ways to create an object. In factory pattern, objects are created without exposing the logic to client and referring to the newly created object using a common interface.


2 Answers

While there may be times when the factory pattern is unnecessary where it may be required in other languages, there are still times when it would be valid to use it - it might just be a way of making your API cleaner - for example as a way of preventing duplication of code that decides which of a series of subclasses to return.

From the Wikipedia article you linked:

Use the factory pattern when:

  • The creation of the object precludes reuse without significantly duplicating code.
  • The creation of the object requires access to information or resources not appropriate to contain within the composing object.
  • The lifetime management of created objects needs to be centralised to ensure consistent behavior.

All of these can still apply when the language is duck typed.

like image 76
actionshrimp Avatar answered Sep 21 '22 20:09

actionshrimp


It's not exactly a factory class, but the Python standard library has at least one factory method: http://docs.python.org/library/collections.html#collections.namedtuple.

And then, of course, there's the fact that you can create classes dynamically using the type() builtin.

I wouldn't say they're meaningless so much as that Python offers a large amount of possibilities for the sort of factories you can create. It's comparatively simple even to write a factory class that creates factory classes as callable instances of itself.

like image 29
JAB Avatar answered Sep 22 '22 20:09

JAB