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


Groups > comp.lang.python > #46397

Getting a callable for any value?

Date 2013-05-29 12:46 -0500
Subject Getting a callable for any value?
From Croepha <croepha@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2367.1369849583.3114.python-list@python.org> (permalink)

Show all headers | View raw


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

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?

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Getting a callable for any value? Croepha <croepha@gmail.com> - 2013-05-29 12:46 -0500
  Re: Getting a callable for any value? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-30 06:41 +0000

csiph-web