<p dir="ltr"><br>
On 29 May 2013 18:51, "Croepha" <<a href="mailto:croepha@gmail.com">croepha@gmail.com</a>> wrote:<br>
><br>
> Is there anything like this in the standard library?<br>
><br>
> class AnyFactory(object):<br>
> def __init__(self, anything):<br>
> self.product = anything<br>
> def __call__(self):<br>
> return self.product<br>
> def __repr__(self):<br>
> return "%s.%s(%r)" % (self.__class__.__module__, self.__class__.__name__, self.product)<br>
><br>
> my use case is: collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None))))<br>
><br>
> And I think lambda expressions are not preferable...<br>
><br>
> I found itertools.repeat(anything).next and functools.partial(copy.copy, anything)<br>
><br>
> but both of those don't repr well... and are confusing... <br>
><br>
> 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?<br>
><br>
></p>
<p dir="ltr">Are you sure you don't want to use a lambda expression? They are pretty pythonic.</p>
<p dir="ltr">none_factory = lambda: None<br>
defaultdict_none_factory = lambda: defaultdict(none_factory)</p>
<p dir="ltr">collections.defaultdict(defaultdict_none_factory)</p>
<p dir="ltr">Just what are you trying to do?</p>