Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python defaultdict use factory method pattern?

Python has a defaultdict class which behaves like a dictionary, except that it outputs a default value when encountering a new key. This default value is created using the default_factory attribute, which is a function that takes no arguments.

For example, d = defaultdict(int) creates a dictionary d whose default value is 0, since int() returns 0.

Why is the default value stored as a function which takes no arguments, rather than storing the default value itself? I am assuming there is a reason behind this choice of implementation, but I don't see what it is. From a naive perspective, it seems like storing the default value would be just as effective and less convoluted.

like image 375
Harry Richman Avatar asked Feb 11 '26 12:02

Harry Richman


1 Answers

Think about how this would work for mutable types.

For example, d = defaultdict(list) creates a new list object for each unassigned key that is requested. If you used a single instance, then each key would be returning the same instance of that list. Appending to an instance returned by one key, e.g. d[1].append(2) would have the observable behavior of appending the value to every other key in the dict. That behavior in almost all cases would be undesirable.

Yes, for primitive/immutable datatypes, like int, float, str, etc, a default value would be acceptable, but that does not cover a huge use-case for containers or other mutable structures where you would want the value for each key to be unique.

like image 136
flakes Avatar answered Feb 13 '26 09:02

flakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!