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: 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 Date: Wed, 31 Aug 2011 09:44:47 -0600 Subject: Re: Handling 2.7 and 3.0 Versions of Dict To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Aug 31, 2011 at 3:55 AM, Martin v. Loewis wrot= e: > 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 =3D dict.itervalues else: getDictValues =3D 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: > =A0def __init__(self, value, hash, eq): > =A0 =A0self.value, self.hash, self.eq =3D value, hash, eq > =A0def __hash__(self): > =A0 =A0return self.hash(self.value) > =A0def __eq__(self, other_key): > =A0 =A0return 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 =3D value def __hash__(self): return hash(keyfunc(self.value)) def __eq__(self, other): return keyfunc(self) =3D=3D keyfunc(other) return Key KeyTypeAlpha =3D Key(lambda x: x % 7) items =3D set(KeyTypeAlpha(value) for value in sourceIterable) Cheers, Ian