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


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

Getting a callable for any value?

Started byCroepha <croepha@gmail.com>
First post2013-05-29 12:46 -0500
Last post2013-05-30 06:41 +0000
Articles 2 — 2 participants

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


Contents

  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

#46397 — Getting a callable for any value?

FromCroepha <croepha@gmail.com>
Date2013-05-29 12:46 -0500
SubjectGetting a callable for any value?
Message-ID<mailman.2367.1369849583.3114.python-list@python.org>

[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?

[toc] | [next] | [standalone]


#46438

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-05-30 06:41 +0000
Message-ID<51a6f47d$0$11118$c3e8da3@news.astraweb.com>
In reply to#46397
On Wed, 29 May 2013 12:46:19 -0500, 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))))

That's not a use-case. That's a code snippet. What does it mean? Why 
would you write such an ugly thing? What does it do? I get a headache 
just looking at it.

I *think* it's a defaultdict that returns a defaultdict on KeyError, 
where the *second* defaultdict returns None.

from collections import defaultdict
defaultdict(lambda: defaultdict(lambda: None))

looks more reasonable to me. I don't know why you need to wrap such a 
simple pair of functions in a class. This is not Java.


http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html

(Twice in one day I have linked to this.)


I'm not sure why you care about the repr of the "AnythingFactory" object. 
You stuff it directly into the defaultdict, where you are very unlikely 
to need to inspect it. You only ever see the defaultdicts they return, 
and they already have a nice repr.



-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web