<div dir="ltr">Is there anything like this in the standard library?<div><br></div><div><div>class AnyFactory(object):</div><div><span class="" style="white-space:pre">     </span>def __init__(self, anything):</div><div><span class="" style="white-space:pre">              </span>self.product = anything</div>
<div><span class="" style="white-space:pre">    </span>def __call__(self):</div><div><span class="" style="white-space:pre">                </span>return self.product</div><div><span class="" style="white-space:pre">        </span>def __repr__(self):</div>
<div><span class="" style="white-space:pre">            </span>return "%s.%s(%r)" % (self.__class__.__module__, self.__class__.__name__, self.product)</div></div><div><br></div><div style>my use case is: collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None))))</div>
<div style><br></div><div style>And I think lambda expressions are not preferable...</div><div style><br></div><div style>I found itertools.repeat(anything).next and functools.partial(copy.copy, anything)</div><div style>
<br></div><div style>but both of those don't repr well... and are confusing...  </div><div style><br></div><div style>I think AnyFactory is the most readable, but is confusing if the reader doesn't know what it is, am I missing a standard implementation of this?</div>
<div style><br></div></div>