Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12491
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!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!newsfeed5.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.018 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'override': 0.07; 'python': 0.08; 'function:': 0.09; 'subject:2.7': 0.09; 'am,': 0.12; 'def': 0.15; 'over:': 0.16; "type's": 0.16; '\xa0def': 0.16; 'wrote:': 0.16; 'wed,': 0.17; 'cheers,': 0.18; '(which': 0.19; 'header:In-Reply-To:1': 0.22; 'noticed': 0.24; 'aug': 0.24; 'specify': 0.24; 'function': 0.27; 'code,': 0.28; 'martin': 0.28; 'message-id:@mail.gmail.com': 0.29; 'hash': 0.30; 'loewis': 0.30; 'class': 0.30; 'actually': 0.33; "can't": 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; '.net': 0.34; 'languages.': 0.34; 'doing': 0.36; 'using': 0.37; 'could': 0.38; 'easiest': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'why': 0.39; 'to:addr:python.org': 0.39; 'your': 0.61; 'custom': 0.61; '31,': 0.64; 'here.': 0.66; 'as:': 0.70; 'factory': 0.73; 'unnecessary': 0.73; '__eq__(self,': 0.84; 'other):': 0.84; 'selector': 0.84; 'self.value': 0.84; 'value):': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=erJEO3APvn07JCZJ9n4GCcJ5JK9SPFLefuQzdOvAgX8=; b=YPfOEg6hyvxeK9d4wDjMbYhhAm7GvpAj9SWWXYwaTeREi8V8r/AYHXHNHmzPyIxbjH BsZh+xe63NQXmYXGQGyZnyPPj1lIjWhBY3J6/cRT/TbCGSgkXUHcXEmshxH7uEdia9EE BQBTb6s/FQwv+6r6fhW6hMy89b1CC5htASxaY= |
| MIME-Version | 1.0 |
| In-Reply-To | <4E5E051B.8060002@v.loewis.de> |
| References | <50924476-9ada-487e-bd4b-5b77bce11d5b@gz5g2000vbb.googlegroups.com> <4E5E051B.8060002@v.loewis.de> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 31 Aug 2011 09:44:47 -0600 |
| Subject | Re: Handling 2.7 and 3.0 Versions of Dict |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| 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.611.1314805519.27778.python-list@python.org> (permalink) |
| Lines | 59 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1314805519 news.xs4all.nl 2419 [2001:888:2000:d::a6]:57160 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:12491 |
Show key headers only | View raw
On Wed, Aug 31, 2011 at 3:55 AM, Martin v. Loewis <martin@v.loewis.de> wrote:
> if sys.version_info < (3,):
> def getDictValues(dict):
> return dict.itervalues()
> else:
> def getDictValues(dict):
> return dict.values()
The extra level of function call indirection is unnecessary here.
Better to write it as:
if sys.version_info < (3,):
getDictValues = dict.itervalues
else:
getDictValues = dict.values
(which is basically what the OP was doing in the first place).
>> I noticed that hashing is a lot different in Python than it is in .NET
>> languages. .NET supports custom "equality comparers" that can override
>> a type's Equals and GetHashCode functions. This is nice when you can't
>> change the class you are hashing. That is why I am using a key
>> selector in my code, here. Is there a better way of overriding the
>> default hashing of a type without actually modifying its definition? I
>> figured a requesting a key was the easiest way.
>
> You could provide a Key class that takes a hash function and a value
> function:
>
> class Key:
> def __init__(self, value, hash, eq):
> self.value, self.hash, self.eq = value, hash, eq
> def __hash__(self):
> return self.hash(self.value)
> def __eq__(self, other_key):
> return self.eq(self.value, other_key.value)
>
> This class would then be used instead of your keySelector.
For added value, you can make it a class factory so you don't have to
specify hash and eq over and over:
def Key(keyfunc):
class Key:
def __init__(self, value):
self.value = value
def __hash__(self):
return hash(keyfunc(self.value))
def __eq__(self, other):
return keyfunc(self) == keyfunc(other)
return Key
KeyTypeAlpha = Key(lambda x: x % 7)
items = set(KeyTypeAlpha(value) for value in sourceIterable)
Cheers,
Ian
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Handling 2.7 and 3.0 Versions of Dict Travis Parks <jehugaleahsa@gmail.com> - 2011-08-30 18:43 -0700
Re: Handling 2.7 and 3.0 Versions of Dict Terry Reedy <tjreedy@udel.edu> - 2011-08-30 23:33 -0400
Re: Handling 2.7 and 3.0 Versions of Dict "Martin v. Loewis" <martin@v.loewis.de> - 2011-08-31 11:55 +0200
Re: Handling 2.7 and 3.0 Versions of Dict Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-31 09:44 -0600
Re: Handling 2.7 and 3.0 Versions of Dict Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-09-01 11:37 +1200
Re: Handling 2.7 and 3.0 Versions of Dict Travis Parks <jehugaleahsa@gmail.com> - 2011-08-31 18:28 -0700
Re: Handling 2.7 and 3.0 Versions of Dict "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-09-02 13:36 -0300
Re: Handling 2.7 and 3.0 Versions of Dict Travis Parks <jehugaleahsa@gmail.com> - 2011-09-02 09:53 -0700
Re: Handling 2.7 and 3.0 Versions of Dict Terry Reedy <tjreedy@udel.edu> - 2011-09-02 16:22 -0400
Re: Handling 2.7 and 3.0 Versions of Dict "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-09-02 17:29 -0300
csiph-web