Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12491
| References | <50924476-9ada-487e-bd4b-5b77bce11d5b@gz5g2000vbb.googlegroups.com> <4E5E051B.8060002@v.loewis.de> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2011-08-31 09:44 -0600 |
| Subject | Re: Handling 2.7 and 3.0 Versions of Dict |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.611.1314805519.27778.python-list@python.org> (permalink) |
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