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


Groups > comp.lang.python > #46397

Getting a callable for any value?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <croepha@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'expressions': 0.07; 'subject:Getting': 0.07; 'library?': 0.09; 'def': 0.12; 'lambda': 0.16; 'repr': 0.16; 'this?': 0.23; 'skip:c 70': 0.24; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'class': 0.32; 'reader': 0.33; 'skip:_ 10': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'anything': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'skip:n 10': 0.64; 'confusing': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=uXeUPyRyOqozdIzdp0NsyRdT56W2OQFfMQS8jqrQeM0=; b=E1f+O7cjxznzFiebVrU268zGYzHFgBQBvrhGVOP44KQI0poVMmQcBDDN/SkddLJcJB uVSTkG6TGK/BZXORQu9F59W4GwX+eKIvqKeV+8K9x5M6NihkvsqO9pf2YOHL/F3UA75P uJ0c/xmq43q9EzwOWMBSIkEgZ4Isx+Ly0JQnnjBA44qIq4grFz3tWEtyZ6GJPWZwb4UI eIgRpQKdkE+tG2rMuGb0wnngT+jidN7LLoO6noSuk/WYTwwHbMa1+ikbiQfvlotHVdqE 3qQQoamkDY0EB+AfdF9dN/ZOxxriDwZlD9efL1dNpCysFIyZnbiEMv1PWzfuggnWlwhY MYRQ==
MIME-Version 1.0
X-Received by 10.50.67.36 with SMTP id k4mr10143701igt.29.1369849579973; Wed, 29 May 2013 10:46:19 -0700 (PDT)
Date Wed, 29 May 2013 12:46:19 -0500
Subject Getting a callable for any value?
From Croepha <croepha@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=047d7bdc18889c339b04dddef5f9
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2367.1369849583.3114.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369849583 news.xs4all.nl 15890 [2001:888:2000:d::a6]:55870
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46397

Show key headers only | 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