Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46400 > unrolled thread

Re: Getting a callable for any value?

Started byFábio Santos <fabiosantosart@gmail.com>
First post2013-05-29 19:19 +0100
Last post2013-05-29 19:19 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Re: Getting a callable for any value? Fábio Santos <fabiosantosart@gmail.com> - 2013-05-29 19:19 +0100

#46400 — Re: Getting a callable for any value?

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-05-29 19:19 +0100
SubjectRe: Getting a callable for any value?
Message-ID<mailman.2370.1369851572.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

On 29 May 2013 18:51, "Croepha" <croepha@gmail.com> wrote:
>
> Is there anything like this in the standard library?
>
> class AnyFactory(object):
> def __init__(self, anything):
> self.product = anything
> def __call__(self):
> return self.product
> def __repr__(self):
> return "%s.%s(%r)" % (self.__class__.__module__, self.__class__.__name__,
self.product)
>
> my use case is:
collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None))))
>
> And I think lambda expressions are not preferable...
>
> I found itertools.repeat(anything).next and functools.partial(copy.copy,
anything)
>
> but both of those don't repr well... and are confusing...
>
> 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?
>
>

Are you sure you don't want to use a lambda expression? They are pretty
pythonic.

none_factory = lambda: None
defaultdict_none_factory = lambda: defaultdict(none_factory)

collections.defaultdict(defaultdict_none_factory)

Just what are you trying to do?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web