Python interpreter bug

F

Fredrik Lundh

No doubt you're right but common sense dictates that membership testing
would test identity not equality.

what does "common sense" have to say about this case:
# True or False ?

</F>
 
S

Steve Holden

I understand this, Steve.
I thought the _cmp_ method was a helper for sorting purposes. Why is it
that a membership test needs to call the __cmp__ method?

Can you suggest another way to test for set membership, given that
instances aren't singletons? The only way to do so is to iterate over
the list, asking "are these the same instance", followed (in the case of
a "no" answer) by "are these two instances equal?".

Consider:
>>> a = {1:'one'}
>>> b = {2:'two'}
>>> c = {1:'one'}
>>> a is c False
>>> a in [b, c] True
>>>

What would you have Python do differently in these circumstances?
If this isn't a bug, it is at least unexpected in my eyes.

Ah, right. So it's your eyes that need fixing! :)
Maybe a candidate for inclusion in the FAQ?
Thank you for answering

A pleasure.

regards
Steve
 
A

alainpoint

Steve said:
Consider:
>>> a = {1:'one'}
>>> b = {2:'two'}
>>> c = {1:'one'}
>>> a is c False
>>> a in [b, c] True
>>>

What would you have Python do differently in these circumstances?

You mean: What i would do i if i was the benevolent dictator ?
I would make a distinction between mutables and immutables. Immutables
would test for equality and mutables would test for identity.
Membership testing for objects is a very common use case which is
totally unrelated to their being sorted according to a key.
I am no expert on languages so i could be wrong. Don't hesitate to
correct me.
Alain
 
S

Steve Holden

Steve Holden wrote:
Consider:
a = {1:'one'}
b = {2:'two'}
c = {1:'one'}
a is c False
a in [b, c] True

What would you have Python do differently in these circumstances?


You mean: What i would do i if i was the benevolent dictator ?
I would make a distinction between mutables and immutables. Immutables
would test for equality and mutables would test for identity.

Which is exactly the wrong way round.
Membership testing for objects is a very common use case which is
totally unrelated to their being sorted according to a key.
I am no expert on languages so i could be wrong. Don't hesitate to
correct me.
Alain
It's not worth bothering - just work with Python how it is, and enjoy
the language!

regards
Steve
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,264
Latest member
FletcherDa

Latest Threads

Top