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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --047d7bdc18889c339b04dddef5f9 Content-Type: text/plain; charset=UTF-8 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? --047d7bdc18889c339b04dddef5f9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Is there anything like this in the standard library?
<= br>
class AnyFactory(object):
def __init__(self, anything):
self.product =3D anything
def __call__(self):=
return self.= product
def __= repr__(self):
return "%s.%s= (%r)" % (self.__class__.__module__, self.__class__.__name__, self.prod= uct)

my use case is: collections.defau= ltdict(AnyFactory(collections.defaultdict(AnyFactory(None))))

And I think lambda expressions are not=C2= =A0preferable...

I found itertools.rep= eat(anything).next and functools.partial(copy.copy, anything)

but both of those don't repr well... and are confu= sing... =C2=A0

I think AnyFactory is t= he most readable, but is confusing if the reader doesn't know what it i= s, am I missing a standard implementation of this?

--047d7bdc18889c339b04dddef5f9--