Handling 2.7 and 3.0 Versions of Dict

Ian Kelly ian.g.kelly at gmail.com
Wed Aug 31 17:44:47 CEST 2011


On Wed, Aug 31, 2011 at 3:55 AM, Martin v. Loewis <martin at 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



More information about the Python-list mailing list