Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46401
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.015 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'expressions': 0.07; 'subject:Getting': 0.07; 'library?': 0.09; 'def': 0.12; 'hint': 0.16; 'key):': 0.16; 'lambda': 0.16; 'repr': 0.16; 'repr.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:c 70': 0.24; 'tells': 0.24; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'is?': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'words,': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'matter': 0.61; 'name': 0.63; 'clearer': 0.84; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=rjo+8tEOm9m+aEQa5W5E3NzSZxZPBbBREuMzkD11WA8=; b=GlL0567igQMLokJxi5Mhzu2KqJ6/9FXF3K/h1Fy7j5bRTi/ToVcXK9aKF8VqUmfJOt U+Rzd7yhbM+vgEslqd4qXOfuM+IUo4yrT84A4yqQ2oTWZemKcoc5sNSpgXkFJq7W9Atw 543+PbHPXkMo6NYkukkbCCuXWT5wgDN5z0v8+6FotCAjN0dh7JAV4g4TYlpgn3Dggd1h hydnt6FBtYFPac0OSNowjUf6HPY1U53QckShtZ5PYACSufIqGZF3/BoNVAK1KQFtflNg KM16iWtsXfzQ+k+bNiYLhwUc3Q9ZlxBhnklDxaH2FjlwIXzwFF7rMcgbj/C1W+fYmfAM 4sbw== |
| X-Received | by 10.66.230.199 with SMTP id ta7mr4742096pac.153.1369851642363; Wed, 29 May 2013 11:20:42 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <CANm61jeHSZL1FQvTr9FS0QKEhFPXug0fibXBEA-4guYF1Lf7UQ@mail.gmail.com> |
| References | <CANm61jeHSZL1FQvTr9FS0QKEhFPXug0fibXBEA-4guYF1Lf7UQ@mail.gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 29 May 2013 12:20:02 -0600 |
| Subject | Re: Getting a callable for any value? |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.2371.1369851646.3114.python-list@python.org> (permalink) |
| Lines | 47 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1369851646 news.xs4all.nl 15891 [2001:888:2000:d::a6]:42727 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:46401 |
Show key headers only | View raw
On Wed, May 29, 2013 at 11:46 AM, Croepha <croepha@gmail.com> 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)
In other words, the function (lambda: x) with a repr that tells you
what x is? Not that I'm aware of, but you could just do something
like:
def return_x(): return x
And then the repr will include the name "return_x", which will give
you a hint as to what it does.
Also, "AnyFactory" is a misleading name because the class above is not
a factory.
> my use case is:
> collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None))))
>
> And I think lambda expressions are not preferable...
What you have above is actually buggy. Your "AnyFactory" will always
return the *same instance* of the passed in defaultdict, which means
that no matter what key you give to the outer defaultdict, you always
get the same inner defaultdict.
Anyway, I think it's clearer with lambdas:
defaultdict(lambda: defaultdict(lambda: None))
But I can see your point about the repr. You could do something like this:
class NoneDefaultDict(dict):
def __missing__(self, key):
return None
def __repr__(self):
return "NoneDefaultDict(%s)" % super(NoneDefaultDict, self).__repr__()
some_dict = defaultdict(NoneDefaultDict)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Getting a callable for any value? Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-29 12:20 -0600
csiph-web