object identity and hashing

J

Jeff Schwab

Can someone explain this?

Create an empty dict and bind it to the name a.

Set the key/value pair (3,):0 to the dict.

Is (3,) one of the keys in the dict?

Yes, it is.

Create two separate tuples (that happen to be equivalent). Are they the
same object?

No, they are not.

Every time you write (3,), you are potentially creating a new object.
These objects have equal values (and hash codes), so they are
interchangeable for purposes of keying a dict.
 
C

castironpi

Can someone explain this?

Create an empty dict and bind it to the name a.

Set the key/value pair (3,):0 to the dict.

Is (3,) one of the keys in the dict?

Yes, it is.

Create two separate tuples (that happen to be equivalent).  Are they the
same object?

No, they are not.

Every time you write (3,), you are potentially creating a new object.
These objects have equal values (and hash codes), so they are
interchangeable for purposes of keying a dict.

I see. You stated,
Is (3,) one of the keys in the dict?


Yes, it is.

It isn't, but it does equal a key that's already in the dict.
 
G

George Sakkis

Create an empty dict and bind it to the name a.
a[(3,)]= 0
Set the key/value pair (3,):0 to the dict.
Is (3,) one of the keys in the dict?

Yes, it is.
Create two separate tuples (that happen to be equivalent). Are they the
same object?

No, they are not.
Every time you write (3,), you are potentially creating a new object.
These objects have equal values (and hash codes), so they are
interchangeable for purposes of keying a dict.

I see. You stated,
Is (3,) one of the keys in the dict?

Yes, it is.

It isn't, but it does equal a key that's already in the dict.

Right, dict lookup depends on object hashing (__hash__) and equality
(__eq__), not identity. There are legitimate cases where lookup should
be based on identity [1], but they are far less common than those
based on equality.

George

[1] http://www.martinfowler.com/eaaCatalog/identityMap.html
 
C

castironpi

(e-mail address removed) wrote:
Can someone explain this?
a= {}
Create an empty dict and bind it to the name a.
a[(3,)]= 0
Set the key/value pair (3,):0 to the dict.
(3,) in a
Is (3,) one of the keys in the dict?
True
Yes, it is.
(3,) is (3,)
Create two separate tuples (that happen to be equivalent).  Are they the
same object?
False
No, they are not.
Every time you write (3,), you are potentially creating a new object.
These objects have equal values (and hash codes), so they are
interchangeable for purposes of keying a dict.
I see.  You stated,
It isn't, but it does equal a key that's already in the dict.

Right, dict lookup depends on object hashing (__hash__) and equality
(__eq__), not identity. There are legitimate cases where lookup should
be based on identity [1], but they are far less common than those
based on equality.

George

[1]http://www.martinfowler.com/eaaCatalog/identityMap.html- Hide quoted text -

- Show quoted text -

[1] illustrates a case in which 'a is a' returns False, and in the
other corner of the DeMorgan table, there is 'a is b' returns True for
'a == b' False.
 
S

Steve Holden

(e-mail address removed) wrote:
Can someone explain this?
a= {}
Create an empty dict and bind it to the name a.
a[(3,)]= 0
Set the key/value pair (3,):0 to the dict.
(3,) in a
Is (3,) one of the keys in the dict?
True
Yes, it is.
(3,) is (3,)
Create two separate tuples (that happen to be equivalent). Are they the
same object?
False
No, they are not.
Every time you write (3,), you are potentially creating a new object.
These objects have equal values (and hash codes), so they are
interchangeable for purposes of keying a dict.
I see. You stated,
Is (3,) one of the keys in the dict?
True
Yes, it is.
It isn't, but it does equal a key that's already in the dict.
Right, dict lookup depends on object hashing (__hash__) and equality
(__eq__), not identity. There are legitimate cases where lookup should
be based on identity [1], but they are far less common than those
based on equality.

George

[1]http://www.martinfowler.com/eaaCatalog/identityMap.html- Hide quoted text -

- Show quoted text -

[1] illustrates a case in which 'a is a' returns False, and in the
other corner of the DeMorgan table, there is 'a is b' returns True for
'a == b' False.
But this doesn't tell you anything about Python except that it's
flexible enough to construct counter-intuitive classes.

Everything you have been told is true for the normal cases you will come
across in everyday usage. If you want to play in the obscure corners of
the language that's fine, but don't try and treat them as mainstream.

regards
Steve
 
C

castironpi

But this doesn't tell you anything about Python except that it's
flexible enough to construct counter-intuitive classes.

Everything you have been told is true for the normal cases you will come
across in everyday usage. If you want to play in the obscure corners of
the language that's fine, but don't try and treat them as mainstream.

Good point.
 
C

castironpi


[1] illustrates a case in which 'a is a' returns False, and in the
other corner of the DeMorgan table, there is 'a is b' returns True for
'a == b' False.

What is the 'a is a' == False case?  I don't see anything like that
illustrated on the Identity Map page.  Is it "P of EAA?"

Good point. I don't know. Just another logical possibility to go in
the 1% of cases. What is P of EAA?

Also, notice: For the list and tuple types, x in y is true if and only
if there exists an index i such that x == y is true-- from the
docs.

And:>>> 'abc' is 'abc'
True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top