Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!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.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'expressions': 0.07; 'subject:Getting': 0.07; 'library?': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'lambda': 0.16; 'lambda:': 0.16; 'repr': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'skip:c 70': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; "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; 'skip:d 20': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:?': 0.36; 'skip:& 10': 0.38; 'anything': 0.39; 'skip:& 20': 0.39; 'sure': 0.39; '8bit%:6': 0.40; 'most': 0.60; 'skip:n 10': 0.64; 'to:addr:gmail.com': 0.65; 'confusing': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:cc:content-type; bh=ymuKAw5/jpd1/BPssz92CpWCdpqAJX5+/AIFqYyTbkM=; b=0MDvGlNC3da4dftcq8k7nUI0HVLXx49/QJ9Ko+XGzA94vSgaVWHRGPfO1OPkOEPPs7 /Z4dFP4KQ0vQJMDDORa1Syn1EbRF2nRFHHhIqxHIEhbuoA7lVkVU1FW07Po+NimO4p78 iYmon01Ny1AlmEmsz13GUpTyt/oikQIWIqD7YWQtNPzp+/1F7IyRoCPJ2HaAUL7O1Yyl EVacEgNl1YMx7dQoCsbXwNBis5aZcEr9eYgK3B+TJwmDbNBdnySoKDeEkg22Ck1bAZDV //M0iWiDQPFN1mx3mxGgFEf2X15xGoXFtcJn7A0u3ULrb+uLl/oqZ9jrKMhtc/hbCSyH f9HA== MIME-Version: 1.0 X-Received: by 10.224.199.3 with SMTP id eq3mr4339893qab.18.1369851568610; Wed, 29 May 2013 11:19:28 -0700 (PDT) Date: Wed, 29 May 2013 19:19:28 +0100 Subject: Re: Getting a callable for any value? From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: Croepha Content-Type: multipart/alternative; boundary=20cf300513e424639904dddf6c9e Cc: python-list@python.org 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: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369851572 news.xs4all.nl 15947 [2001:888:2000:d::a6]:41733 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46400 --20cf300513e424639904dddf6c9e Content-Type: text/plain; charset=ISO-8859-1 On 29 May 2013 18:51, "Croepha" 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? --20cf300513e424639904dddf6c9e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


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 =3D anything
> def __call__(self):
> return self.product
> def __repr__(self):
> return "%s.%s(%r)" % (self.__class__.__module__, self.__clas= s__.__name__, self.product)
>
> my use case is: collections.defaultdict(AnyFactory(collections.default= dict(AnyFactory(None))))
>
> And I think lambda expressions are not=A0preferable...
>
> I found itertools.repeat(anything).next and functools.partial(copy.cop= y, anything)
>
> but both of those don't repr well... and are confusing... =A0
>
> I think AnyFactory is the most readable, but is confusing if the reade= r doesn't know what it is, am I missing a standard implementation of th= is?
>
>

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

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

collections.defaultdict(defaultdict_none_factory)

Just what are you trying to do?

--20cf300513e424639904dddf6c9e--